Tadas Melnikas
Tadas Melnikas

Reputation: 97

How to plot a histogram in Python for more than 2 columns?

I have a df on customer behavior, looking like this:

customer_id segment      pagea   pageb   pagec   paged    ...
01            male        2         3       0        6  
02            female      3         0       0        2
03            female      4         10      2        15
04            male        8         11      3        7
05            male        0         0       0        0  
06            female      1         0       2        1
 ...        ...

I want to create a histogram showing the distribution of clicking on each page per segment (male and female). I only know how to do it for two columns:

df.set_index('a')['segment'].plot.bar()

How do I create on multiple columns?

Upvotes: 0

Views: 66

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC, did you want something like this:

df.melt(['customer_id', 'segment'])\
  .set_index(['variable','segment'])\
  .sum(level=[0,1]).unstack()['value'].plot.bar()

Output:

enter image description here

Upvotes: 1

Related Questions