Search code examples
pythonpandasstructure

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 e.g., 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)


Solution

  • 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"]])