[英]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:
我想要做的是使用遍历列表的 foreach 循环,并为列表中的每个项目从数据框中删除相应的行:
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'). Fruitveg_df 这个包含很多列和行(有一列标签为“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?
有人可以帮忙吗?
我的猜测是你需要:
fruitveg_df = fruitveg_df[~fruitveg_df['2004'].isin(irrelevantList)]
让我们调用df
您的数据框并说您想删除列fruit
项目
df = df[~df["fruit"].isin(irrelevantList)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.