【问题标题】:Groupby and sum and insert as a new row with previous columnsGroupby 和 sum 并作为新行插入前列
【发布时间】:2020-07-09 13:59:09
【问题描述】:

我正在重组数据。该过程包含两个任务:

  1. 通过对 groupby 求和插入新行。
  2. 创建一个新列Level

我在图 1 和图 2 中插入了预期的输出。

数据框包含多个列。示例数据框如下:

df = pd.DataFrame()
df ['Competition']= ['EPL','EPL','EPL','EPL','EPL','EPL','EPL','EPL','EPL','EPL']
df ['Player']= ['Bruno','Bruno','Bruno','Bruno','Bruno','Bruno','Bruno','Bruno','Bruno','Bruno']
df ['template'] = ['Def','Def','Pass','Pass','Actions','Actions','Attk','Attk','Other','Other']
df ['Stats'] = ['Def duels', ' ','Back passes', ' ','Dribbles', ' ','Goal','Assist','Possession Losses','Possession Losses [own half]']
df ['Stat1'] = [' ', 'Def duels Won',' ', 'Back passes[Acc]',' ', 'Dribbles[Suc]',' ',' ',' ',' ']
df ['Value'] = [5,2.5,60,55,5,2,2,1,3,1]

我想通过 Competition、Player、Template 列使用 group 对值求和。该值将作为新行插入到现有行的正上方。预期的数据框如下:

基于上述日期框架,我想创建一个新列Level如下:

> The Level as defined as follow: level= 1 if blank or no data in the
> columns **Stats,Stats1** level= 2 if blank or no data in the columns
> **Stats1** level= 3 if data in the columns **Stats1**

我该怎么做?

【问题讨论】:

  • 最后一行看起来不对
  • 嗨对不起,它应该是 2。我会更新它。谢谢

标签: python pandas pandas-groupby transform


【解决方案1】:

这是np.select,您可以根据需要进行修改:

# add extra rows with concat
df = pd.concat((df, df.groupby(['Competition','Player','template'])
                 .Value.sum().reset_index()
               )
         ).fillna(' ')

df['Level'] = np.select((df['Stat1'].ne(' '), df['Stats'].ne(' ')),
                        (3, 2), 1)

输出:

    Competition    Player    template    Stats                         Stat1               Value    Level
--  -------------  --------  ----------  ----------------------------  ----------------  -------  -------
 0  EPL            Bruno     Def         Def duels                                           5          2
 1  EPL            Bruno     Def                                       Def duels Won         2.5        3
 2  EPL            Bruno     Pass        Back passes                                        60          2
 3  EPL            Bruno     Pass                                      Back passes[Acc]     55          3
 4  EPL            Bruno     Actions     Dribbles                                            5          2
 5  EPL            Bruno     Actions                                   Dribbles[Suc]         2          3
 6  EPL            Bruno     Attk        Goal                                                2          2
 7  EPL            Bruno     Attk        Assist                                              1          2
 8  EPL            Bruno     Other       Possession Losses                                   3          2
 9  EPL            Bruno     Other       Possession Losses [own half]                        1          2
 0  EPL            Bruno     Actions                                                         7          1
 1  EPL            Bruno     Attk                                                            3          1
 2  EPL            Bruno     Def                                                             7.5        1
 3  EPL            Bruno     Other                                                           4          1
 4  EPL            Bruno     Pass                                                          115          1

【讨论】:

  • 嗨,第一部分如何,groupby sum 并作为新行插入?谢谢
  • 也为此发布您预期的输出数据框。视觉是无价的
  • @Zephyr 查看更新。不过,您需要重新排列数据。
  • 我在两张图片中发布了预期的数据帧。谢谢
猜你喜欢
  • 2018-12-02
  • 1970-01-01
  • 2019-09-29
  • 2010-10-30
  • 2016-05-14
  • 2021-08-25
  • 2016-12-23
  • 1970-01-01
相关资源
最近更新 更多