Home > Software design >  How to create a Pandas dataframe from another column in a dataframe by splitting it?
How to create a Pandas dataframe from another column in a dataframe by splitting it?

Time:05-15

I have the following source dataframe

Person Country Is Rich?
0 US Yes
1 India No
2 India Yes
3 US Yes
4 US Yes
5 India No
6 US No
7 India No

I need to convert it another dataframe for plotting a bar graph like below for easily accessing data

enter image description here

CodePudding user response:

You can try pivot_table

df['Is Rich?'] = df['Is Rich?'].replace({'Yes': 'Rich', 'No': 'Poor'})
out = df.pivot_table(index='Country', columns='Is Rich?', values='Person', aggfunc='count')
print(out)

Is Rich?  Poor  Rich
Country
India        3     1
US           1     3
  • Related