This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: DataFrame operations

DataFrame filtering exercise

face Oana Cocarascu Josiah Wang

Filtering is likely to be an operation you will use a lot if you are using pandas. So let’s practise a few!

Task 1

Print all Pokemons that have "Water" as Type 1 and "Dragon" as Type 2.

>>> filtered_df = ?????
>>> print(filtered_df)
           # Type 1  Type 2  ...  Speed  Generation  Legendary
Name                         ...
Kingdra  230  Water  Dragon  ...     85           2      False
Palkia   484  Water  Dragon  ...    100           4       True

[2 rows x 12 columns]

>>> filtered_df = df[(df["Type 1"] == "Water") & (df["Type 2"] == "Dragon")]
>>> print(filtered_df)
           # Type 1  Type 2  ...  Speed  Generation  Legendary
Name                         ...
Kingdra  230  Water  Dragon  ...     85           2      False
Palkia   484  Water  Dragon  ...    100           4       True

[2 rows x 12 columns]