我有一个pandas数据帧,其条目都是字符串:
A B C
1 apple banana pear
2 pear pear apple
3 banana pear pear
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
我想选择包含某个字符串的所有行,比如'banana'.我不知道每次都会出现哪一列.当然,我可以写一个for循环并迭代所有行.但有更简单或更快的方法吗?
Div*_*kar 18
在选择行的核心,我们需要一个一维掩码或一个长度与长度相同的熊猫系列布尔元素df
,我们称之为mask
。因此,最后使用df[mask]
,我们将在boolean-indexingdf
之后删除选定的行。
这是我们的开始df
:
In [42]: df
Out[42]:
A B C
1 apple banana pear
2 pear pear apple
3 banana pear pear
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
现在,如果我们只需要匹配一个字符串,就可以直接使用元素相等:
In [42]: df == 'banana'
Out[42]:
A B C
1 False True False
2 False False False
3 True False False
4 False False False
Run Code Online (Sandbox Code Playgroud)
如果我们需要ANY
在每一行中查找一个匹配项,请使用.any
方法:
In [43]: (df == 'banana').any(axis=1)
Out[43]:
1 True
2 False
3 True
4 False
dtype: bool
Run Code Online (Sandbox Code Playgroud)
要选择相应的行:
In [44]: df[(df == 'banana').any(axis=1)]
Out[44]:
A B C
1 apple banana pear
3 banana pear pear
Run Code Online (Sandbox Code Playgroud)
1. 搜索ANY
匹配
这是我们的开始df
:
In [42]: df
Out[42]:
A B C
1 apple banana pear
2 pear pear apple
3 banana pear pear
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
NumPynp.isin
将在这里工作(或使用其他帖子中列出的 pandas.isin)从df
. 所以,假设我们正在寻找'pear'
或'apple'
在df
:
In [51]: np.isin(df, ['pear','apple'])
Out[51]:
array([[ True, False, True],
[ True, True, True],
[False, True, True],
[ True, True, True]])
# ANY match along each row
In [52]: np.isin(df, ['pear','apple']).any(axis=1)
Out[52]: array([ True, True, True, True])
# Select corresponding rows with masking
In [56]: df[np.isin(df, ['pear','apple']).any(axis=1)]
Out[56]:
A B C
1 apple banana pear
2 pear pear apple
3 banana pear pear
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
2. 搜索ALL
匹配
我们df
又要开始了:
In [42]: df
Out[42]:
A B C
1 apple banana pear
2 pear pear apple
3 banana pear pear
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
所以,现在我们正在寻找有BOTH
say 的行['pear','apple']
。我们将利用NumPy-broadcasting
:
In [66]: np.equal.outer(df.to_numpy(copy=False), ['pear','apple']).any(axis=1)
Out[66]:
array([[ True, True],
[ True, True],
[ True, False],
[ True, True]])
Run Code Online (Sandbox Code Playgroud)
所以,我们有一个2
项目的搜索列表,因此我们有一个带有number of rows = len(df)
和的二维掩码number of cols = number of search items
。因此,在上面的结果中,我们有第一个 col for'pear'
和第二个col for 'apple'
。
为了使事情具体化,让我们为三个项目准备一个掩码['apple','banana', 'pear']
:
In [62]: np.equal.outer(df.to_numpy(copy=False), ['apple','banana', 'pear']).any(axis=1)
Out[62]:
array([[ True, True, True],
[ True, False, True],
[False, True, True],
[ True, False, True]])
Run Code Online (Sandbox Code Playgroud)
此掩码的列'apple','banana', 'pear'
分别为。
回到2
搜索项目案例,我们之前有:
In [66]: np.equal.outer(df.to_numpy(copy=False), ['pear','apple']).any(axis=1)
Out[66]:
array([[ True, True],
[ True, True],
[ True, False],
[ True, True]])
Run Code Online (Sandbox Code Playgroud)
因为,我们正在寻找ALL
每一行的匹配项:
In [67]: np.equal.outer(df.to_numpy(copy=False), ['pear','apple']).any(axis=1).all(axis=1)
Out[67]: array([ True, True, False, True])
Run Code Online (Sandbox Code Playgroud)
最后,选择行:
In [70]: df[np.equal.outer(df.to_numpy(copy=False), ['pear','apple']).any(axis=1).all(axis=1)]
Out[70]:
A B C
1 apple banana pear
2 pear pear apple
4 apple apple pear
Run Code Online (Sandbox Code Playgroud)
如果您希望该行的所有行都df
包含中的任何值values
,请使用:
df[df.isin(values).any(1)]
Run Code Online (Sandbox Code Playgroud)
例子:
In [2]: df
Out[2]:
0 1 2
0 7 4 9
1 8 2 7
2 1 9 7
3 3 8 5
4 5 1 1
In [3]: df[df.isin({1, 9, 123}).any(1)]
Out[3]:
0 1 2
0 7 4 9
2 1 9 7
4 5 1 1
Run Code Online (Sandbox Code Playgroud)
对于单个搜索值
df[df.values == "banana"]
Run Code Online (Sandbox Code Playgroud)
要么
df[df.isin(['banana'])]
Run Code Online (Sandbox Code Playgroud)
对于多个搜索词:
df[(df.values == "banana")|(df.values == "apple" ) ]
Run Code Online (Sandbox Code Playgroud)
要么
df[df.isin(['banana', "apple"])]
# A B C
# 1 apple banana NaN
# 2 NaN NaN apple
# 3 banana NaN NaN
# 4 apple apple NaN
Run Code Online (Sandbox Code Playgroud)
从Divakar:返回两者的行。
select_rows(df,['apple','banana'])
# A B C
# 0 apple banana pear
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14992 次 |
最近记录: |