Programming Lesson and Exercises-Pandas 8d


123
views
0
6 months ago by
The assignment is 'Define a boolean mask countries_in_EU_not_RUS for countries in Europe without Russia.
(Hint: Reuse the boolean mask countries_in_EU.
The alpha_3 code of Russia is RUS.)'.My code is as follows:

countries_in_EU_not_RUS = countries_in_EU['alpha_3'] != 'RUS'
country_data[countries_in_EU_not_RUS]

Although a similar question is asked in the theory, my code gives a KeyError: 'alpha_3'. What does this mean?

1 Answer


0
6 months ago by
That is because countries_in_EU is only a mask and does not include the column alpha_3 (so it raises an error - check that for yourself: countries_in_EU.head()). For that reason try to combine both masks separately:
countries_in_EU = country_data['continent'] == 'EU' # condition 1 (defined earlier)
countries_not_RUS = country_data['alpha_3'] != 'RUS' # condition 2

countries_in_EU_not_RUS = countries_in_EU & countries_not_RUS # combine condition 1 and 2
Please login to add an answer/comment or follow this question.

Similar posts:
Search »