import pandas as pd import numpy as np sex_ratios = pd.read_csv('m2f_ratios.csv', skiprows=8) age_code = {a: i for i,a in enumerate(sex_ratios.Age.unique())} age_label = {i: a for i,a in enumerate(sex_ratios.Age.unique())} sex_ratios['AgeCode'] = sex_ratios.Age.apply(lambda x: age_code[x]) area_idx = sex_ratios.Area == \ 'United Kingdom of Great Britain and Northern Ireland' years_idx = sex_ratios.Year <= 2015 sex_ratios_uk = sex_ratios[np.logical_and(years_idx, area_idx)]Here take care of the age coding and isolate the data for the United Kingdom and Northern Ireland. Now we can rearrange the data to see ratio per year and age using a pivot table, we can then visualize the result using the heatmap function from seaborn:

import matplotlib as plt import seaborn as sns pivot_uk = sex_ratios_uk.pivot_table(values='Ratio', index='AgeCode', columns='Year') pivot_uk.index = [age_label[a] for a in pivot_uk.index.values] plt.figure(figsize=(10, 8)) plt.title('Sex ratio per year and age groups') sns.heatmap(pivot_uk, annot=True) plt.show()

In each year we see that the ratio was above 1 (in favor of males) for young ages it then becomes lower than 1 during adulthood and keeps lowering with the age. It also seems that with time the ratio decreases more slowly. For example, we see that the age group 70-74 had a ratio of 0.63 in 1970, while the ratio in 2015 was 0.9.

I think you forgot "import matplotlib as plt"

ReplyDeleteGreat job and thank you ;)

I actually did, thanks!

Delete