我有一个数据框。以下代码有效
stat = working_data.groupby(by=['url', 'bucket_id'],
as_index=False).agg({'delta': 'max','id': 'count'})
现在,我需要计算具有不同状态的ID。我的状态为“下载”,“否下载”和“下载”。
我想有df
同列bucket_id
,max
,downloaded
(有多少人“已下载”状态), not_downloaded
(有多少人“NOT_DOWNLOADED”状态), downloading
(有多少“正在下载”状态)。怎么做?
输入我有:。
输出我有:
如您所见,计数不是由状态划分的。但是我想知道对于每个bucket_id bucket_id,都有x个下载,y not_downloaded,z个下载(因此它们应该在单独的列中,但是一个bucket_id的信息应该在一行中)
一种使用assign创建列然后聚合此新列的方法。
working_data.assign(downloaded=df['status'] == 'DOWNLOADED',
not_downloaded=df['status'] == 'NOT_DOWNLOADED',
downloading=df['status'] == 'DOWNLOADING')\
.groupby(by=['url', 'bucket_id'],
as_index=False).agg({'delta': 'max',
'id': 'count',
'downloaded': 'sum',
'not_donwloaded':'sum',
'downloading':'sum'})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句