![](/img/trans.png)
[英]Drop rows on multiple conditions (based on 2 column) in pandas dataframe
[英]On Pandas, how to drop multiple rows based in a column
我是Pandas的新手,我正嘗試從Column country
刪除包含相應國家(阿爾巴尼亞,烏茲別克斯坦,巴西)的所有行。 但是,我想出的方法是一個接一個地完成,如下所示:
indexCountry = df[df['country'] == 'Albania'].index
df.drop(indexCountry, inplace = True)
indexCountry = df[df['country'] == 'Uzbekistan'].index
df.drop(indexCountry, inplace = True)
indexCountry = df[df['country'] == 'Brazil'].index
df.drop(indexCountry, inplace = True)
有沒有一種方法可以在一個代碼行中做到這一點,而不必為每個國家/地區做一個?
您可以像這樣進行過濾:
df = df[~df["country"].isin(["Alabania", "Uzbekistan", "Brazil"])]
~
是其后跟的否定。
嘗試:
list_of_countries = ['Albania', 'Uzbekistan', 'Brazil']
indexCountry = df[df['country'].isin(list_of_countries)].index
df.drop(indexCountry, inplace = True)
or just:
list_of_countries = ['Albania', 'Uzbekistan', 'Brazil']
df[~df["country"].isin(list_of_countries)]
您還可以使用以下命令:
df = df[~df.country.str.contains('|'.join(["Albania","Uzbekistan","Brazil"]))]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.