Pythonic sintax with dict indexing. Best practices? | 137-7

I usually do not work indexing and filtering dictionaries but the syntax below feels incredibly verbose. Is there a way to write it cleaner? Even if it involves concepts we have not learned yet?

data['demographics'] = data['demographics'][data['demographics']['schoolyear'] == 20112012]


1 Like

Fala Renan, tudo certo?

The problem here is that we’re applying a boolean mask to DataFrame that is inside a dictionary, no only indexing a dictionary or a DataFrame. If that was the case, the syntax would be much simpler.

What we are doing with this code is the same as:

new_df = df[df['schoolyear'] == 20112012]

Also, I’m moving this to the Q&A section.

1 Like