4 回答
![?](http://img1.sycdn.imooc.com/54584f3100019e9702200220-100-100.jpg)
TA贡献1807条经验 获得超9个赞
你可以用pd.Series.isin
。
对于“IN”:( somewhere.isin(something)
读:是否something
在somewhere
?)
或者“不在”: ~somewhere.isin(something)
举个例子:
>>> 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
![?](http://img1.sycdn.imooc.com/5458478b0001f01502200220-100-100.jpg)
TA贡献1804条经验 获得超2个赞
我一直在对这样的行进行泛型过滤:
criterion = lambda row: row['countries'] not in countries
not_in = df[df.apply(criterion, axis=1)]
![?](http://img1.sycdn.imooc.com/5333a1d100010c2602000200-100-100.jpg)
TA贡献1831条经验 获得超10个赞
如何实现in和not in一个pandas DataFrame?
:熊猫提供了两种方法Series.isin,并DataFrame.isin分别对系列和DataFrames。这是titular python运算符到它们等效的pandas操作的映射。
╒════════╤══════════════════════╤══════════════════════╕
│ │ Python │ Pandas │
╞════════╪══════════════════════╪══════════════════════╡
│ in │ item in sequence │ sequence.isin(item) │
├────────┼──────────────────────┼──────────────────────┤
│ not in │ item not in sequence │ ~sequence.isin(item) │
╘════════╧══════════════════════╧══════════════════════╛
要实现“not in”,必须反转结果isin。
另请注意,在pandas情况下,“ sequence”可以引用Series或DataFrame,而“ item”本身可以是可迭代的(很快就会更多)。
基于ONE Column过滤DataFrame(也适用于Series)
最常见的情况是isin在特定列上应用条件以过滤DataFrame中的行。
df = pd.DataFrame({'countries': ['US', 'UK', 'Germany', np.nan, 'China']})
df
countries
0 US
1 UK
2 Germany
3 China
c1 = ['UK', 'China'] # list
c2 = {'Germany'} # set
c3 = pd.Series(['China', 'US']) # Series
c4 = np.array(['US', 'UK']) # array
Series.isin接受各种类型作为输入。以下是获得所需内容的所有有效方法:
df['countries'].isin(c1)
0 False
1 True
2 False
3 False
4 True
Name: countries, dtype: bool
# `in` operation
df[df['countries'].isin(c1)]
countries
1 UK
4 China
# `not in` operation
df[~df['countries'].isin(c1)]
countries
0 US
2 Germany
3 NaN
# Filter with `set` (tuples work too)
df[df['countries'].isin(c2)]
countries
2 Germany
# Filter with another Series
df[df['countries'].isin(c3)]
countries
0 US
4 China
# Filter with array
df[df['countries'].isin(c4)]
countries
0 US
1 UK
过滤多个列
有时,您会希望对多列使用某些搜索字词进行“入”成员资格检查,
df2 = pd.DataFrame({
'A': ['x', 'y', 'z', 'q'], 'B': ['w', 'a', np.nan, 'x'], 'C': np.arange(4)})
df2
A B C
0 x w 0
1 y a 1
2 z NaN 2
3 q x 3
c1 = ['x', 'w', 'p']
要将isin条件应用于“A”和“B”列,请使用DataFrame.isin:
df2[['A', 'B']].isin(c1)
A B
0 True True
1 False False
2 False False
3 False True
从这里,为了保留至少有一列的行True,我们可以any沿第一轴使用:
df2[['A', 'B']].isin(c1).any(axis=1)
0 True
1 False
2 False
3 True
dtype: bool
df2[df2[['A', 'B']].isin(c1).any(axis=1)]
A B C
0 x w 0
3 q x 3
请注意,如果要搜索每个列,则只需省略列选择步骤即可
df2.isin(c1).any(axis=1)
同样,要保留ALL列所在的行True,请使用all与以前相同的方式。
df2[df2[['A', 'B']].isin(c1).all(axis=1)]
A B C
0 x w 0
添加回答
举报