简体   繁体   English

如何通过拆分从数据框中的另一列创建 Pandas 数据框?

[英]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 0 US我们 Yes是的
1 1 India印度 No
2 2 India印度 Yes是的
3 3 US我们 Yes是的
4 4 US我们 Yes是的
5 5 India印度 No
6 6 US我们 No
7 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 3 1 1
India印度 1 1 3 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.

相关问题 如何基于另一个DataFrame中的列在Pandas DataFrame中创建新列? - How to create a new column in a Pandas DataFrame based on a column in another DataFrame? 使用来自另一个数据帧的 if 条件在 Pandas 数据帧中创建一个新列 - create a new column in pandas dataframe using if condition from another dataframe 使用另一个数据框创建熊猫数据框列 - Create pandas dataframe column using another dataframe 如何根据另一个 DataFrame 中的列更新 Pandas DataFrame 中的列 - How to update a column in pandas DataFrame based on column from another DataFrame 如何从另一列的所有值创建新的列名并按 pandas dataframe 中的另一列创建新列名? - how to create new column names from another column all values and agg by another column in pandas dataframe? 如何基于多列从另一个 dataframe 中提取 pandas dataframe? - how to extract pandas dataframe from another dataframe based on multiple column? 如何将 dataframe 中的每一列与另一个 dataframe pandas 的行相乘? - How to multiply each column in a dataframe with a row from another dataframe pandas? 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe 从另一个具有字典键的列创建pandas dataframe列 - Create pandas dataframe column from another column that has dictionary keys 根据另一列拆分pandas DataFrame列的最短方法 - Shortest way of splitting a pandas DataFrame column based on another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM