Writing a For loop to delete words in a list from a dataframe

Question:

I have a listof strings:

non_dogs =['tiger_shark', 'upright', 'walking_stick', 'water_bottle']

i want to delete the strings in that list from the dataframe, how do i do that using a for loop using a code like this:

clean_breeds =clean_images[(clean_images['dog_breed']== 'tiger_shark')].index
clean_images.drop(clean_breeds,inplace = True)

I tried writing a for loop but it was not working

Asked By: Miebi Williams

||

Answers:

To delete strings in a list non_dogs from a dataframe clean_images, you can use a for loop and the drop method of a dataframe. The code would look like this:

for breed in non_dogs:
    clean_breeds = clean_images[(clean_images['dog_breed'] == breed)].index
    clean_images.drop(clean_breeds, inplace=True)

This code will loop through each string in the non_dogs list. For each iteration, it will find all the rows in the dataframe clean_images where the value in the dog_breed column is equal to the current string. The indices of those rows will be stored in the variable clean_breeds. Then, those rows will be dropped from the dataframe using the drop method. The inplace=True argument makes sure that the changes are made to the dataframe itself, rather than creating a new dataframe with the changes.

Here’s an example to help illustrate the code:

import pandas as pd

# Create a sample dataframe
clean_images = pd.DataFrame({'dog_breed': ['tiger_shark', 'labrador', 'walking_stick', 'beagle', 'water_bottle']})

# Create a list of strings to delete from the dataframe
non_dogs = ['tiger_shark', 'walking_stick', 'water_bottle']

# Use a for loop to delete the strings in the list from the dataframe
for breed in non_dogs:
    clean_breeds = clean_images[(clean_images['dog_breed'] == breed)].index
    clean_images.drop(clean_breeds, inplace=True)

# The resulting dataframe only contains rows with dog breeds
print(clean_images)

The output of this code will be:

  dog_breed
1  labrador
3    beagle