Python 组合值_计数
我有一个由df.column.value\u counts().sort\u index()生成的熊猫系列 我期望的输出是Python 组合值_计数,python,pandas,counting,bin,Python,Pandas,Counting,Bin,我有一个由df.column.value\u counts().sort\u index()生成的熊猫系列 我期望的输出是 | bin | Total | |-------|--------| | 0-13 | 3522 | | 14-26 | 793 | | 27-50 | 9278 | 我尝试了df.column.value\u counts(bins=3.sort\u index()),但没有成功 我可以用计算机得到正确的结果 a = df.column.value
| bin | Total |
|-------|--------|
| 0-13 | 3522 |
| 14-26 | 793 |
| 27-50 | 9278 |
我尝试了df.column.value\u counts(bins=3.sort\u index()),但没有成功
我可以用计算机得到正确的结果
a = df.column.value_counts().sort_index()[:14].sum()
b = df.column.value_counts().sort_index()[14:27].sum()
c = df.column.value_counts().sort_index()[28:].sum()
print(a, b, c)
Output: 3522 793 9270
但是我想知道是否有一种方法可以满足我的需求。欢迎提供任何建议。:-) 您可以使用
pd.cut
:
pd.cut(df['N Months'], [0,13, 26, 50], include_lowest=True).value_counts()
更新您应该能够将自定义bin传递给
值\u计数
:
df['N Months'].value_counts(bins = [0,13, 26, 50])
输出:
N Months
(-0.001, 13.0] 3522
(13.0, 26.0] 793
(26.0, 50.0] 9278
Name: Count, dtype: int64
通过列表是一个很好的答案!
df['N Months'].value_counts(bins = [0,13, 26, 50])
N Months
(-0.001, 13.0] 3522
(13.0, 26.0] 793
(26.0, 50.0] 9278
Name: Count, dtype: int64