pandas 实现 in 和 not in 的用法及心得

经常在处理数据中从一个总数据中清洗出数据, 但是有时候需要把没有处理的数据也统计出来.

这时候就需要使用:

pandas.DataFrame.isin

DataFrame中的每个元素是否都包含在值中

pandas文档位置

例子:

如何实现SQL的等价物IN和NOT IN?

我有一个包含所需值的列表。下面是一个场景:

df = pd.DataFrame({'countries':['US','UK','Germany','China']})
countries = ['UK','China']

# pseudo-code:
df[df['countries'] not in countries]

之前的做法是这样:

df = pd.DataFrame({'countries':['US','UK','Germany','China']})
countries = pd.DataFrame({'countries':['UK','China'], 'matched':True})

# IN
df.merge(countries,how='inner',on='countries')

# NOT IN
not_in = df.merge(countries,how='left',on='countries')
not_in = not_in[pd.isnull(not_in['matched'])]

但上面这样做觉得很不好, 也翻了文档才找到比较好解决方式.

# IN
something.isin(somewhere)

# NOT IN
~something.isin(somewhere)

例子:

>>> df
  countries
0        US
1        UK
2   Germany
3     China
>>> countries
['UK', 'China']
>>> df.countries.isin(countries)
0    False
1     True
2    False
3     True
Name: countries, dtype: bool
>>> df[df.countries.isin(countries)]
  countries
1        UK
3     China
>>> df[~df.countries.isin(countries)]
  countries
0        US
2   Germany

 

 

你可能感兴趣的:(Pandas数据分析)