我想要一個餅圖來比較幸存者的年齡組。問題是我不知道如何計算同齡人。正如您在螢屏截圖底部看到的,它表示 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)
輸出

uj5u.com熱心網友回復:
呼叫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()

uj5u.com熱心網友回復:
- 將
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/353575.html標籤:Python 熊猫 matplotlib 条形图 饼形图
上一篇:圖例未出現在3D圖中
