Search code examples
pythonpandascountfrequency

Count the frequency that a bunch of values occurs in a dataframe column


pretty new to python and pandas, i have a 15000 values in a column of my dataframe like this.

col1 col2
5 0.05964
19 0.00325
31 0.0225
12 0.03325
14 0.00525

I want to get in output a result like this :

0.00 to 0.01 = 55 values, 
0.01 to 0.02 = 365 values, 
0.02 to 0.03 = 5464 values etc... from 0.00 to 1.00

Im a bit lost with groupby or count.values etc...

thanks for the help !


Solution

  • IIUC, use pd.cut:

    out = df.groupby(pd.cut(df['col2'], np.linspace(0, 1, 101)))['col1'].sum()
    print(out)
    
    # Output
    col2
    (0.0, 0.01]     33
    (0.01, 0.02]     0
    (0.02, 0.03]    31
    (0.03, 0.04]    12
    (0.04, 0.05]     0
                    ..
    (0.95, 0.96]     0
    (0.96, 0.97]     0
    (0.97, 0.98]     0
    (0.98, 0.99]     0
    (0.99, 1.0]      0
    Name: col1, Length: 100, dtype: int64