简体   繁体   中英

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

As output I get the df with hidden rows of non EU countries (eg. indexing starts with 6). How can I remove them entirely from my dataframe?

If indexing is the issue, you could reset the index after slicing your 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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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