[英]Pandas groupby -> aggregate - function of two columns
I'm using pandas aggregate
as folows:我正在使用 pandas aggregate
如下:
In [6]: gb = df.groupby(['col1', 'col2'])
...: counts = gb.size().to_frame(name='counts')
...: (counts
...: .join(gb.agg({'col3': 'mean'}).rename(columns={'col3': 'col3_mean'}))
...: .join(gb.agg({'col4': 'median'}).rename(columns={'col4': 'col4_median'}))
...: .join(gb.agg({'col4': 'min'}).rename(columns={'col4': 'col4_min'}))
...: .reset_index()
...: )
How can I add one more column which will contain sum of values col3 * col4
?如何再添加一列,其中包含值col3 * col4
的总和?
First create column new
before groupby
and then aggregate sum
, your solution rewritten in named aggregation is:首先在groupby
之前创建 column new
然后聚合sum
,您在命名聚合中重写的解决方案是:
counts = (df.assign(new = df['col3'] * df['col4'])
.groupby(['col1', 'col2'], as_index=False)
.agg(counts=('col1','size'),
col3_mean=('col3','mean'),
col4_median=('col4','median'),
col4_min=('col4','min'),
both_sum=('new','sum')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.