Screen Link:
My Code:
mean_happiness = {}
regions = happiness2015['Region'].unique()
for r in regions:
happiness2015['Region'] == r
happiness2015[r]
if this works,
happiness2015[happiness2015['Region'] == r]
how come my code way does not work? I’m just trying to understand the logic behind this.
What actually happened:
KeyError: 'Western Europe'
1 Like
This is called boolean indexing here you are indexing happiness2015
with boolean values True
or False
. You can print print(happiness2015['Region'] == r)
to confirm its values.
for more about Boolean indexing - pandas doc
laurenk9304:
happiness2015[r]
While here you are indexing through raw string which gave KeyError
exception if key does not found.
1 Like
Thanks for the answer.
While here you are indexing through raw string which gave KeyError exception if key does not found.
But what do you mean by raw string?
I mean r
contains string value.