[英]How to aggregate group metrics and plot data with pandas
我想要一个饼图来比较幸存者的年龄组。 问题是我不知道如何计算同龄人。 正如您在屏幕截图底部看到的,它显示 142 列。 但是,数据集中有 891 人。
import pandas as pd
import seaborn as sns # for test data only
# load test data from seaborn
df_t = sns.load_dataset('titanic')
# capitalize the column headers to match code used below
df_t.columns = df_t.columns.str.title()
dft = df_t.groupby(['Age', 'Survived']).size().reset_index(name='count')
def get_num_people_by_age_category(dft):
dft["age_group"] = pd.cut(x=dft['Age'], bins=[0,18,60,100], labels=["young","middle_aged","old"])
return dft
# Call function
dft = get_num_people_by_age_category(dft)
print(dft)
输出
调用df_t.groupby(['Age', 'Survived']).size().reset_index(name='count')
创建一个数据df_t.groupby(['Age', 'Survived']).size().reset_index(name='count')
,每个年龄和每个幸存状态一行。
要获得每个年龄组的计数,可以将“年龄组”列添加到原始数据框中。 在下一步中, groupby
可以使用该“年龄组”。
from matplotlib import pyplot as plt
import seaborn as sns # to load the titanic dataset
import pandas as pd
df_t = sns.load_dataset('titanic')
df_t["age_group"] = pd.cut(x=df_t['age'], bins=[0, 18, 60, 100], labels=["young", "middle aged", "old"])
df_per_age = df_t.groupby(['age_group', 'survived']).size().reset_index(name='count')
labels = [f'{age_group},\n {"survived" if survived == 1 else "not survived"}'
for age_group, survived in df_per_age[['age_group', 'survived']].values]
labels[-1] = labels[-1].replace('\n', ' ') # remove newline for the last items as the wedges are too thin
labels[-2] = labels[-2].replace('\n', ' ')
plt.pie(df_per_age['count'], labels=labels)
plt.tight_layout()
plt.show()
pandas.DataFrame.plot
和kind='bar'
来完成。pandas.crosstab
重塑数据,这会在两个因素之间创建频率交叉表。matplotlib.pyplot.bar_label
包含条形注释
import pandas as pd
import seaborn as sns
# load data
df = sns.load_dataset('titanic')
df.columns = df.columns.str.title()
# map 0 and 1 of Survived to a string
df.Survived = df.Survived.map({0: 'Died', 1: 'Survived'})
# bin the age
df['Age Group'] = pd.cut(x=df['Age'], bins=[0, 18, 60, 100], labels=['Young', 'Middle Aged', 'Senior'])
# Calculate the counts
ct = pd.crosstab(df['Survived'], df['Age Group'])
# display(ct)
Age Group Young Middle Aged Senior
Survived
Died 69 338 17
Survived 70 215 5
# plot
ax = ct.plot(kind='bar', rot=0, xlabel='')
# optionally add annotations
for c in ax.containers:
ax.bar_label(c, label_type='edge')
# pad the spacing between the number and the edge of the figure
ax.margins(y=0.1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.