将作为 groupby 操作的结果的行插入到原始数据帧中

Sat*_*ate 4 python group-by dataframe pandas pandas-groupby

例如,我有一个熊猫数据框,如下所示:

col_1   col_2   col_3  col_4
a       X        5      1
a       Y        3      2
a       Z        6      4
b       X        7      8
b       Y        4      3
b       Z        6      5
Run Code Online (Sandbox Code Playgroud)

我想,对于 col_1 中的每个值,添加 col_3 和 col_4(以及更多列)中对应于 col_2 中的 X 和 Z 的值,并使用这些值创建一个新行。所以输出如下:

col_1   col_2   col_3  col_4 
a       X        5      1
a       Y        3      2
a       Z        6      4
a       NEW      11     5
b       X        7      8
b       Y        4      3
b       Z        6      5
b       NEW      13     13
Run Code Online (Sandbox Code Playgroud)

此外,col_1 中可能有更多需要相同处理的值,因此我无法明确引用“a”和“b”。我尝试使用 groupby('col_1') 和 apply() 的组合,但我无法让它工作。我与下面的内容非常接近,但我无法将“新”放入 col_2 并将原始值(a 或 b 等)保留在 col_1 中。

df.append(df[(df['col_2'] == 'X') | (df['col_2'] == 'Z')].groupby('col_1').mean())
Run Code Online (Sandbox Code Playgroud)

谢谢。

cs9*_*s95 5

如果你能保证X并且Z在一个组中只出现一次,你可以使用groupbyandpd.concat操作:

new = df[df.col_2.isin(['X', 'Z'])]\
      .groupby(['col_1'], as_index=False).sum()\
      .assign(col_2='NEW')

df = pd.concat([df, new]).sort_values('col_1')

df
  col_1 col_2  col_3  col_4
0     a     X      5      1
1     a     Y      3      2
2     a     Z      6      4
0     a   NEW     11      5
3     b     X      7      8
4     b     Y      4      3
5     b     Z      6      5
1     b   NEW     13     13
Run Code Online (Sandbox Code Playgroud)