假设我有一个 DataFrame,其中有一列名为 col1。如果我想获得 col1 == 'a' 的所有行,我可以这样做:
df[df.col1 == ‘a’]
Run Code Online (Sandbox Code Playgroud)
如果我想要 col1 为“a”或“b”的行,我可以这样做:
df[(df.col1 == ‘a’) | (df.col1 == ‘b’)]
Run Code Online (Sandbox Code Playgroud)
但我真的很想做这样的事情(在语法上是非法的):
df[df.col1 in [‘a’, ‘b’, ‘c’]]
Run Code Online (Sandbox Code Playgroud)
有没有合适的熊猫方法来做到这一点?
这是我正在使用的:
sort_func = lambda x: x in [‘a’, ‘b’, ‘c’]
mask = df[‘col1’].apply(sort_func)
df[mask]
Run Code Online (Sandbox Code Playgroud)
但是……有没有更好的方法来做到这一点?这让我很困扰。
Zer*_*ero 14
使用isin()进行过滤
In [212]: df = pd.DataFrame([['a', 1], ['b', 2], ['c', 3], ['d', 4]],
columns=['col1', 'col2'])
In [213]: df['col1'].isin(['a', 'b', 'c'])
Out[213]:
0 True
1 True
2 True
3 False
Name: col1, dtype: bool
In [214]: df.ix[df['col1'].isin(['a', 'b', 'c']), :]
Out[214]:
col1 col2
0 a 1
1 b 2
2 c 3
Run Code Online (Sandbox Code Playgroud)