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
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

