[英]Apply function to grouped data counts in pandas
>>> new_confirmIOC.groupby(['ErrorCode','ResponseType']).OrderID.count()
ErrorCode ResponseType
0 CANCEL_ORDER_CONFIRM 80
TRADE_CONFIRM 31
1 CANCEL_ORDER_CONFIRM 80
TRADE_CONFIRM 31
如何为错误代码0加上总计的百分比,例如80 / 111、31 / 111,依此类推
我试过了
new_confirmIOC.groupby(['ErrorCode','ResponseType']).OrderID.count().apply(lambda x: x / x.sum())
但这给了我
ErrorCode ResponseType
0 CANCEL_ORDER_CONFIRM 1
TRADE_CONFIRM 1
1 CANCEL_ORDER_CONFIRM 1
TRADE_CONFIRM 1
Name: OrderID, dtype: int64
我认为您需要按第一层groupby
并按sum
除法:
df = new_confirmIOC.groupby(['ErrorCode','ResponseType']).OrderID.count()
df = df.groupby(level='ErrorCode').apply(lambda x: x / x.sum())
print (df)
ErrorCode ResponseType
0 CANCEL_ORDER_CONFIRM 0.720721
TRADE_CONFIRM 0.279279
1 CANCEL_ORDER_CONFIRM 0.720721
TRADE_CONFIRM 0.279279
Name: val, dtype: float64
transform
另一种解决方案:
df = df.div(df.groupby(level='ErrorCode').transform('sum'))
print (df)
ErrorCode ResponseType
0 CANCEL_ORDER_CONFIRM 0.720721
TRADE_CONFIRM 0.279279
1 CANCEL_ORDER_CONFIRM 0.720721
TRADE_CONFIRM 0.279279
Name: val, dtype: float64
谢谢FLab发表评论:
.count
的结果是一个Series,因此apply函数将逐个元素地操作。 (不像熊猫DataFrame那样在整列上)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.