Search code examples
pandasbin

How to bin data from multiple columns?


I have the following df

|   1   |   2   |   3   |
-------------------------
  0.11     0.25    0.74
  0.32     0.93    0.26
  0.44     0.28    0.76
  0.15     0.29    0.79

etc.

I'm using bins:

bins = [0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1]

I created 3 bin columns and ran a value_counts() on them. So now I know how many values are in each bin for each of these 3 columns. But I'm having trouble plotting this into a barplot. Looking for a triple bar graph

df['Bin1'] = pd.cut(df['1'], bins)
df['Bin2'] = pd.cut(df['2'], bins)
df['Bin3'] = pd.cut(df['3'], bins)
Bin1_count = df['Bin1'].value_counts().values
Bin2_count = df['Bin2'].value_counts().values
Bin3_count = df['Bin3'].value_counts().values
x_axis = df['Bin1'].value_counts().index

sns.barplot(x = x_axis, y = [Bin1_count,Bin2_count,Bin3_count])

Solution

  • You can using melt first , then using pd.crosstab, and try look at plot from pandas

    meltdf=df.melt()
    meltdf.value=pd.cut(meltdf.value,bins)
    pd.crosstab(meltdf.variable,meltdf.value).plot(kind='bar')