You’re right, i was a bit short in my explanation, (and it’s a bit nitpicky to check if the given order is correct
)
women_degrees[org_stem_cats].iloc[41]
returns a Series for the last year in the dataset (the 42nd row) for the 6 stem degrees:
Math and Statistics 43.100000
Computer Science 18.200000
Engineering 17.500000
Biology 58.742397
Physical Sciences 40.100000
Psychology 76.700000
Name: 41, dtype: float64
100-women_degrees[org_stem_cats].iloc[41]
returns the inverse, so the share of men in the stem degrees.
Math and Statistics 56.900000
Computer Science 81.800000
Engineering 82.500000
Biology 41.257603
Physical Sciences 59.900000
Psychology 23.300000
Name: 41, dtype: float64
These two subtracted is the difference between men and women (the gender gap) in each of these degrees in the last year:
Math and Statistics 13.800000
Computer Science 63.600000
Engineering 65.000000
Biology -17.484794
Physical Sciences 19.800000
Psychology -53.400000
Name: 41, dtype: float64
Since we’re only interested in the absolute differences between men and women, i used the abs() function to do that:
(100-women_degrees[org_stem_cats].iloc[41]-women_degrees[org_stem_cats].iloc[41]).abs()
Math and Statistics 13.800000
Computer Science 63.600000
Engineering 65.000000
Biology 17.484794
Physical Sciences 19.800000
Psychology 53.400000
Name: 41, dtype: float64
The last step is to order the values in a descending order:
(100-women_degrees[org_stem_cats].iloc[41]-women_degrees[org_stem_cats].iloc[41]).abs().sort_values(ascending=False)
Engineering 65.000000
Computer Science 63.600000
Psychology 53.400000
Physical Sciences 19.800000
Biology 17.484794
Math and Statistics 13.800000
Name: 41, dtype: float64
Then you can use this Series as an index in the same way as the given stem_cats list.
org_stem_cats = ['Math and Statistics', 'Computer Science', 'Engineering', 'Biology', 'Physical Sciences', 'Psychology']
stem_cats = (100-women_degrees[org_stem_cats].iloc[41]-women_degrees[org_stem_cats].iloc[41]).abs().sort_values(ascending=False).index
fig = plt.figure(figsize=(18, 3))
for sp in range(0,6):
ax = fig.add_subplot(1,6,sp+1)
ax.plot(women_degrees['Year'], women_degrees[stem_cats[sp]], c=cb_dark_blue, label='Women', linewidth=3)
ax.plot(women_degrees['Year'], 100-women_degrees[stem_cats[sp]], c=cb_orange, label='Men', linewidth=3)
for key,spine in ax.spines.items():
spine.set_visible(False)
ax.set_xlim(1968, 2011)
ax.set_ylim(0,100)
ax.set_title(stem_cats[sp])
ax.tick_params(bottom="off", top="off", left="off", right="off")
plt.show()
My order:
The expected order: