Python Pandas – Select rows based on partial string matches

In this post we will be creating a Python script that will select rows within a data frame based on partial string matches. This means if a column partially contains a string, the entire row will be returned.

See below a sample of our data frame for this example.

In this example we will be returning all rows that contain ‘Wii’ within the column named ‘Platform’. To do this we would use the snippet of code below.

Matches = df[df['Platform'].str.contains("Wii")]

The above would return the following as output.

   Rank                       Name Platform
0     1                 Wii Sports      Wii
2     3             Mario Kart Wii      Wii
3     4          Wii Sports Resort      Wii
7     8                   Wii Play      Wii
8     9  New Super Mario Bros. Wii      Wii

Leave a Reply