Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading