我有以下数据框
name,value
a,100
b,200
c,150
d,300
e,400
f,200
g,100
我有范围列表,例如 [0-100,100-200,超过 200]
基于此,我必须计算上面数据框中的记录数。
我需要像这样的输出
category,count
0-100,2
100-200,3
mor than 200,3
Use groupby + cut:
bins = [-1, 100, 200, np.inf]
labels=['0-100','100-200','more than 200']
df=df.groupby(pd.cut(df['value'], bins=bins, labels=labels)).size().reset_index(name='count')
print (df)
value count
0 0-100 2
1 100-200 3
2 more than 200 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)