【问题标题】:To find sum and percentage from columns of two different dataframe and append result in third dataframe从两个不同数据帧的列中查找总和和百分比并将结果附加到第三个数据帧
【发布时间】:2019-04-23 10:11:27
【问题描述】:

我制作了 2 个外观相同的数据框,如下所示:

df1:

date          id       email              Count
4/22/2019       1     abc@xyz.com           10
4/22/2019       1     def@xyz.com           4
4/23/2019       1     abc@xyz.com           5
4/23/2019       1     def@xyz.com           10


df2:

date          id    Email_ID           Count
4/22/2019       1   fgh@xyz.com         5
4/22/2019       1   ijk@xyz.com         6
4/23/2019       1   fgh@xyz.com         7
4/23/2019       1   ijk@xyz.com         8

我想制作一个 dataframe3,其中包含每个数据帧(df1 和 df2)的“计数”列的总和和百分比,并根据日期计算单个百分比 [like df1_count%=(df1_count/df1_count+df2_count)*100]。输出 df3 应如下所示:

df3:
                   Count                     Count%
date        df1_count   df2_count    df1_count%     df2_count%
 4/22/2019    14            11          56%            44%
 4/23/2019    15            15          50%            50%

pandas 是怎么做到的?我可以使用“for”循环来做到这一点,但不能通过 pandas 功能来做到这一点,任何线索都会有所帮助


根据解决方案@jezrael 输出

          Count       Count         count%         count%
         df1_count   df2_count    df1_count%     df2_count%
Date
4/22/2019     14            11          56%            44%

2019 年 4 月 23 日 15 15 50% 50%

【问题讨论】:

  • 我的解决方案效果如何?
  • @jezrael 非常感谢它的工作。我可以在最终输出中将 df1_count 和 df2_count 作为主列 'Count' 下的子列,并将 df1_count% 和 df_count% 作为主列 'Count%' 下的子列吗?
  • 你能改变预期的输出吗?
  • 或者在df = df.reset_index()之前需要df.columns = df.columns.str.split('_', expand=True).swaplevel()
  • @jezrael 我已经更改了输出格式

标签: python-3.x pandas


【解决方案1】:

concat 与聚合sum 一起使用:

df = pd.concat([df1.groupby('date')['Count'].sum(),
                df2.groupby('date')['Count'].sum()], axis=1, keys=('df1_count','df2_count'))

然后添加新列:

s = (df['df1_count'] + df['df2_count'])
df['df1_count%'] = df['df1_count'] / s * 100
df['df2_count%'] = df['df2_count'] / s * 100
df = df.reset_index()
print (df)
        date  df1_count  df2_count  df1_count%  df2_count%
0  4/22/2019         14         11        56.0        44.0
1  4/23/2019         15         15        50.0        50.0

如果需要百分比值首先转换为stringsSeries.round 用于截断小数:

s = (df['df1_count'] + df['df2_count'])
df['df1_count%'] = (df['df1_count'] / s * 100).round().astype(str) + '%'
df['df2_count%'] = (df['df2_count'] / s * 100).round().astype(str) + '%'
df = df.reset_index()
print (df)
        date  df1_count  df2_count df1_count% df2_count%
0  4/22/2019         14         11      56.0%      44.0%
1  4/23/2019         15         15      50.0%      50.0%

编辑:

df = pd.concat([df1.groupby('date')['Count'].sum(),
                df2.groupby('date')['Count'].sum()], axis=1, 
                keys=('Count_df1_count','Count_df2_count'))

s = (df['Count_df1_count'] + df['Count_df2_count'])
df['Count%_df1_count%'] = (df['Count_df1_count'] / s * 100).round().astype(str) + '%'
df['Count%_df2_count%'] = (df['Count_df2_count'] / s * 100).round().astype(str) + '%'
df.columns = df.columns.str.split('_', expand=True, n=1)
print (df)

              Count               Count%           
          df1_count df2_count df1_count% df2_count%
date                                               
4/22/2019        14        11      56.0%      44.0%
4/23/2019        15        15      50.0%      50.0%

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2019-06-23
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多