user7740495
user7740495

Reputation:

droping rows based on the values in cells by iterating through a list of values in pandas

I have a list of values and a dataframe. What I want to do is use a foreach loop going through the list and for each item in the list drop the corresponding row from the dataframe:

irrelevantList=['Blackcurrants', 'Cherries', '', 'Damsons', 'Gooseberries', 'Pears', 'Plums and Gages',]

fruitveg_df this one contains lots of colums and rows(there is a column with label '2004').

I tired this:

for item in irrelevantList:
    fruitveg_df.drop(fruitveg_df['2004']==item, inplace=True)

but it is not working. Can somebody help please?

Upvotes: 1

Views: 33

Answers (2)

zipa
zipa

Reputation: 27879

My guess is that you need:

fruitveg_df = fruitveg_df[~fruitveg_df['2004'].isin(irrelevantList)]

Upvotes: 1

rpanai
rpanai

Reputation: 13447

Let's call df your dataframe and say you want to remove items on column fruit

df = df[~df["fruit"].isin(irrelevantList)]

Upvotes: 0

Related Questions