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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Ch3n   2023-02-03 我要评论

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

ps:pandas实现in和 not in

pandas中经常会需要对某列做一些筛选,比如筛选某列里的不包含某些值的行,类似sql里的in和not in功能,那么怎么实现呢。

import pandas as pd
columns = ['name','country']
index = [1,2,3,4]
row1 = ['a','China']
row2 = ['b','UK']
row3 = ['c','USA']
row4 = ['d','HK']

df = pd.DataFrame([row1,row2,row3,row4],
                   index=index,
                   columns=columns)
df

chinese = ['China','HK']

那么想查看数据中是chines的,

df[df.country.isin(chinese)]

查看数据中不是chines的,

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们