Source From Here
Question
I have a DataFrame column with numeric values:
I want to see the column as bin counts:
How can I get the result as bins with their value counts?
HowTo
You can leverage API pandas.cut to bin the target column:
Then you can leverage another API DataFrame.groupby to calculate the count:
Or:
Output:
I have a DataFrame column with numeric values:
- import pandas as pd
- df = pd.DataFrame([[46.5], [44.2], [100.0], [42.12]], columns=['percentage'])
- bins = [0, 1, 5, 10, 25, 50, 100]
HowTo
You can leverage API pandas.cut to bin the target column:
Then you can leverage another API DataFrame.groupby to calculate the count:
Or:
- df['binned'].value_counts()
- 25-50 3
- 50-100 1
- 10-25 0
- 5-10 0
- 1-5 0
- 0-10 0
- Name: binned, dtype: int64
沒有留言:
張貼留言