Search code examples
python-3.xpandasdata-processing

Need to plot histogram in Pandas such that x axis is categorical and y axis is sum of some column


I have a data frame in Pandas (using Python 3.7) as shown below:

#     actuals  probability   bucket
# 0      0.0     0.116375      2
# 1      0.0     0.239069      3   
# 2      1.0     0.591988      6
# 3      0.0     0.273709      3
# 4      1.0     0.929855      10

Where 'bucket' can take discreet values from 1 to 10. And 'actuals' can take only 2 values, either 1 or 0. I need to plot a histogram such that x-axis = 'bucket' (i.e 1 to 10) and y-axis = Sum of 'actuals' . Then how can I do that?


Solution

  • Use groupby.sum with plot:

    df.groupby('bucket')['actuals'].sum().plot(kind='bar')
    

    If need histogram use kind='hist'