简体   繁体   中英

Plot each column in a dataframe?

I have a dataframe with 45 columns (see below). I would like to plot each column as a separate bar graph. I'm very new to Python and can't figure out how to do this. I think the problem might be the way the dataframe is set up with the row names.

        V1_category V2_category V3_category V4_category V5_category V6_category
Neutral 78.378378   83.783784   59.459459   27.027027   54.054054   32.432432
Painful 0.000000    0.000000    0.000000    2.702703    2.702703    13.513514   
Pleasant8.108108    10.810811   2.702703    16.216216   5.405405    2.702703
Unpleasant13.513514 5.405405    37.837838   54.054054   37.837838   51.351351" 

One way could be:

# Import libraries
import pandas as pd
import matplotlib.pyplot as plt

# Create DataFrame
df = pd.DataFrame({
    'V1_category': [78.378378,0.0,8.108108,13.513514],
    'V2_category': [83.783784,0.0,10.810811,5.405405],
    'V3_category': [59.459459,0.0,2.702703,37.837838],
    'V4_category': [27.027027,2.702703,16.216216,54.054054],
    'V5_category': [54.054054,2.702703,5.405405,37.837838],
    'V6_category': [32.432432, 13.513514,2.702703, 51.351351]
    }, index =  ['Neutral','Painful','Pleasant','Unpleasant']
)
df

# Using pandas 'df.plot'
for col in df.columns:
    df[col].plot(kind='bar')
    plt.title(col)
    plt.show()

Only two plots shown below:

在此处输入图像描述

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