繁体   English   中英

使用列值更新 Pandas groupby 组

[英]update pandas groupby group with column value

我有一个这样的测试 df:

df = pd.DataFrame({'A': ['Apple','Apple', 'Apple','Orange','Orange','Orange','Pears','Pears'],
                    'B': [1,2,9,6,4,3,2,1]
                   })
       A    B
0   Apple   1
1   Apple   2
2   Apple   9
3   Orange  6
4   Orange  4
5   Orange  3
6   Pears   2
7   Pears   1

现在我需要在 col 'B' 中添加一个具有各自 %differences 的新列。 这怎么可能。 我不能让它工作。

我查看了 pandas groupby().last() 的更新列值不确定它是否与我的问题有关。

这看起来很有希望Pandas Groupby 和 Sum Only One Column

我需要找到并插入 col maxpercchng(组中的所有行)中每组 col 'A' 的 col (B) 的最大变化。 所以我想出了这个代码:

grouppercchng = ((df.groupby['A'].max() - df.groupby['A'].min())/df.groupby['A'].iloc[0])*100

并尝试将它添加到组 col 'maxpercchng' 像这样

group['maxpercchng'] = grouppercchng

或者像这样

df_kpi_hot.groupby(['A'], as_index=False)['maxpercchng'] = grouppercchng

有谁知道如何将 maxpercchng col 添加到组中的所有行?

我相信您需要对具有与由聚合值填充的原始 DataFrame 相同大小的系列进行transform

g = df.groupby('A')['B']
df['maxpercchng'] = (g.transform('max') - g.transform('min')) /  g.transform('first') * 100

print (df)

        A  B  maxpercchng
0   Apple  1        800.0
1   Apple  2        800.0
2   Apple  9        800.0
3  Orange  6         50.0
4  Orange  4         50.0
5  Orange  3         50.0
6   Pears  2         50.0
7   Pears  1         50.0

或者:

g = df.groupby('A')['B']
df1 = ((g.max() - g.min()) / g.first() * 100).reset_index()
print (df1)

        A      B
0   Apple  800.0
1  Orange   50.0
2   Pears   50.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM