简体   繁体   中英

How to create a single pie chart from multiple columns in a pandas data-frame

I have a data frame which looks like

     Q1_1  Q1_2  Q1_3  Q1_4  Q1_5  Q1_6  Q1_7  Q1_8  Q1_9  Q1_10
0     0     0     1     0     1     0     0     0     0      0
1     0     0     0     0     0     0     0     0     1      0
2     0     0     0     0     0     1     0     0     1      0
3     1     0     1     0     1     0     0     0     0      0
4     0     0     1     0     0     0     0     0     0      0

The columns describes the options for a question eg, professor (Q1_1), baker (Q1_2), butcher (Q1_3), plumber (Q1_4), etc.

What I would like is to show the absolute and relative frequencies using a pie-chart and a bar chart stating the absolute values and the percentages of all the columns so in this case there will be 10 pieces of the pie and 10 bars; for example for col= Q1_5, the values will be 20% (2 items)

You need to sum up the columns to get absolute frequencies. Assuming you use pandas, you can use df.sum() for that. Then you can plot the results with matplotlib.

For a quick bar chart you could use

df.sum().plot(kind=bar)

I figured it out.

First you need to "flatten all the columns in to one column":

df['Q1_all'] = (df.iloc[:, 0:] == 1).idxmax(1)

Then just apply whatever function:

cross_tab_Q1_group = pd.crosstab([df.Q1_all], [df.group])
cross_tab_Q1_group.plot(kind="barh", stacked=True, color=[sns.xkcd_rgb['medium green'], sns.xkcd_rgb["pale red"], sns.xkcd_rgb["denim blue"]])

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