调用对象
apply的“得分”列,
groupby并使用vectorise
str方法
contains,使用此方法过滤
group和调用
count:
In [34]: df.groupby(['catA', 'catB'])['scores'].apply(lambda x: x[x.str.contains('RET')].count())Out[34]:catA catBA X 1 Y 1B Z 2Name: scores, dtype: int64
要将其分配为列使用,
transform以便聚合返回其索引与原始df对齐的序列:
In [35]:df['count'] = df.groupby(['catA', 'catB'])['scores'].transform(lambda x: x[x.str.contains('RET')].count())dfOut[35]: catA catB scores count0 A X 6-4 RET 11 A X 6-4 6-4 12 A Y 6-3 RET 13 B Z 6-0 RET 24 B Z 6-1 RET 2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)