I want to have a pie chart that compares survived people's age groups. The problem is I don't know how to count people with the same age. As you see in the bottom of screenshot, it says 142 columns. But, there are 891 people in the dataset.
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)
output