[英]How to create a Pandas dataframe from another column in a dataframe by splitting it?
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我需要将其转换为另一个数据框以绘制如下所示的条形图,以便轻松访问数据
Bar chart of economic status per country每个国家的经济状况条形图
Data frame to be created is like below.要创建的数据框如下所示。
Country![]() |
Rich![]() |
Poor![]() |
---|---|---|
US![]() |
3 ![]() |
1 ![]() |
India![]() |
1 ![]() |
3 ![]() |
I am new to Pandas and Exploratory data science.我是 Pandas 和探索性数据科学的新手。 Please help here
请在这里帮忙
You can try pivot_table
你可以试试
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
You could do:你可以这样做:
converted = df.assign(Rich=df['Is Rich?'].eq('Yes')).eval('Poor = ~Rich').groupby('Country').agg({'Rich': 'sum', 'Poor': 'sum'})
print(converted)
Rich Poor
Country
India 1 3
US 3 1
However, if you want to plot it as a barplot, the following format might work best with a plotting library like seaborn
:但是,如果要将其绘制为条形图,则以下格式可能最适合使用
seaborn
类的绘图库:
plot_df = converted.reset_index().melt(id_vars='Country', value_name='No. of people', var_name='Status')
print(plot_df)
Country Status No. of people
0 India Rich 1
1 US Rich 3
2 India Poor 3
3 US Poor 1
Then, with seaborn
:然后,使用
seaborn
:
import seaborn as sns
sns.barplot(x='Country', hue='Status', y='No. of people', data=plot_df)
Resulting plot:结果图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.