简体   繁体   English

根据条件从数据框中删除行

[英]deleting rows from data frame based on a condition

I need to delete names of the countries which do not belong to EU from a data frame.我需要从数据框中删除不属于欧盟的国家的名称。 I applied this part of code:我应用了这部分代码:

 df=df[df['COUNTRY'].isin(EU)]

wheras EU is a list of EU countries其中 EU 是欧盟国家的列表

As output I get the df with hidden rows of non EU countries (eg. indexing starts with 6).作为 output 我得到了带有非欧盟国家隐藏行的 df (例如,索引从 6 开始)。 How can I remove them entirely from my dataframe?如何从我的 dataframe 中完全删除它们?

If indexing is the issue, you could reset the index after slicing your dataframe.如果索引是问题,您可以在切片 dataframe 后重置索引。
Like this:像这样:

df = df[df['COUNTRY'].isin(EU)].reset_index(drop=True)

You can filter out rows with country not in the EU list by using ~ for inversion:您可以使用~进行反转来过滤掉国家不在欧盟列表中的行:

df = df[~df['COUNTRY'].isin(EU)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM