简体   繁体   中英

plot histogram for many columns quickly using groupby function of pandas dataframe

I have a pandas dataframe, which looks like 在此处输入图片说明 Here one of column is named label , that can take only two possible values 0 or 1.

I would like to make histogram for label 1 and for label 0 separately one top of other, like

在此处输入图片说明

I am able to make this for one of the column (named "MA_CL05") like:

temp = infile.groupby('label')
for k, v in temp:
  if k == 1:
    v.MA_CL05.hist(label='1',figsize=(15,15),bins=25,alpha=1.0,histtype = 'step',lw=4)
  if k == 0:
    v.MA_CL05.hist(label='0',figsize=(15,15),bins=25,alpha=1.0,histtype = 'step',lw=4)
plt.legend(loc=1, prop={'size': 51})
plt.show()

I can copy and past this patch for all of 20 columns and it will be fine. But, is there any easy way to plot this histogram of type ( 2 ) in one go?

You can add another loop, looping about the columns of the dataframe and specifying the axes to plot to.

fig, axes = plt.subplots(4,5)

for col,ax in zip(infile.columns[2:],axes.flatten()):
    temp = infile.groupby('label')
    for k, v in temp:
        v[col].hist(label=str(k),bins=25,alpha=1.0,histtype = 'step',lw=4, ax=ax)

plt.legend(loc=1, prop={'size': 51})
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM