假设我们在Python Pandas中有一个数据框,如下所示:
df = pd.DataFrame({'vals': [1, 2, 3, 4], 'ids': [u'aball', u'bball', u'cnut', u'fball']})
或者,以表格形式:
ids vals
aball 1
bball 2
cnut 3
fball 4
如何过滤包含关键词"ball?"的行?例如,输出应为:
ids vals
aball 1
bball 2
fball 4
Amit..
223
In [3]: df[df['ids'].str.contains("ball")]
Out[3]:
ids vals
0 aball 1
1 bball 2
3 fball 4
Jubbles..
69
df[df['ids'].str.contains('ball', na = False)] # valid for (at least) pandas version 0.17.1
逐步解释(从内到外):
df['ids']
选择ids
数据框的列(技术上,对象df['ids']
是类型pandas.Series
)
df['ids'].str
允许我们将向量化的字符串方法(例如lower
,contains
)应用于Series
df['ids'].str.contains('ball')
检查Series的每个元素,以确定元素值是否将字符串'ball'作为子字符串.结果是一系列布尔指示True
或表示False
存在"球"子串.
df[df['ids'].str.contains('ball')]
将布尔"掩码"应用于数据帧并返回包含适当记录的视图.
na = False
从考虑中删除NA/NaN值; 否则可能会返回ValueError.
user3820991..
9
>>> mask = df['ids'].str.contains('ball')
>>> mask
0 True
1 True
2 False
3 True
Name: ids, dtype: bool
>>> df[mask]
ids vals
0 aball 1
1 bball 2
3 fball 4
Cleb..
7
如果要将筛选的列设置为新索引,还可以考虑使用.filter
; 如果你想把它作为一个单独的列保留,那么str.contains
就是要走的路.
让我们说你有
df = pd.DataFrame({'vals': [1, 2, 3, 4, 5], 'ids': [u'aball', u'bball', u'cnut', u'fball', 'ballxyz']})
ids vals
0 aball 1
1 bball 2
2 cnut 3
3 fball 4
4 ballxyz 5
并且您的计划是过滤ids
包含ball
AND设置ids
为新索引的所有行,您可以这样做
df.set_index('ids').filter(like='ball', axis=0)
这使
vals
ids
aball 1
bball 2
fball 4
ballxyz 5
但filter
也允许您传递正则表达式,因此您也可以只过滤列条目结尾的那些行ball
.在这种情况下你使用
df.set_index('ids').filter(regex='ball$', axis=0)
vals
ids
aball 1
bball 2
fball 4
请注意,现在条目ballxyz
不包括在内,因为它开头ball
并且不以它结束.
如果您想获得所有以ball
您开头的条目,可以简单使用
df.set_index('ids').filter(regex='^ball', axis=0)
生产
vals
ids
ballxyz 5
同样适用于列; 你需要改变的只是axis=0
部分.如果你根据列进行过滤,那就是axis=1
.
1> Amit..:
In [3]: df[df['ids'].str.contains("ball")]
Out[3]:
ids vals
0 aball 1
1 bball 2
3 fball 4
@ user4896331 - `df [~df ['ids'].str.contains("ball")]`,`~`否定条件
你怎么反过来找到所有不包含字符串的行?
2> Jubbles..:
df[df['ids'].str.contains('ball', na = False)] # valid for (at least) pandas version 0.17.1
逐步解释(从内到外):
df['ids']
选择ids
数据框的列(技术上,对象df['ids']
是类型pandas.Series
)
df['ids'].str
允许我们将向量化的字符串方法(例如lower
,contains
)应用于Series
df['ids'].str.contains('ball')
检查Series的每个元素,以确定元素值是否将字符串'ball'作为子字符串.结果是一系列布尔指示True
或表示False
存在"球"子串.
df[df['ids'].str.contains('ball')]
将布尔"掩码"应用于数据帧并返回包含适当记录的视图.
na = False
从考虑中删除NA/NaN值; 否则可能会返回ValueError.
当有人做一步一步的解释时,绝对喜欢它.这真的有助于理解!
3> user3820991..:
>>> mask = df['ids'].str.contains('ball')
>>> mask
0 True
1 True
2 False
3 True
Name: ids, dtype: bool
>>> df[mask]
ids vals
0 aball 1
1 bball 2
3 fball 4
4> Cleb..:
如果要将筛选的列设置为新索引,还可以考虑使用.filter
; 如果你想把它作为一个单独的列保留,那么str.contains
就是要走的路.
让我们说你有
df = pd.DataFrame({'vals': [1, 2, 3, 4, 5], 'ids': [u'aball', u'bball', u'cnut', u'fball', 'ballxyz']})
ids vals
0 aball 1
1 bball 2
2 cnut 3
3 fball 4
4 ballxyz 5
并且您的计划是过滤ids
包含ball
AND设置ids
为新索引的所有行,您可以这样做
df.set_index('ids').filter(like='ball', axis=0)
这使
vals
ids
aball 1
bball 2
fball 4
ballxyz 5
但filter
也允许您传递正则表达式,因此您也可以只过滤列条目结尾的那些行ball
.在这种情况下你使用
df.set_index('ids').filter(regex='ball$', axis=0)
vals
ids
aball 1
bball 2
fball 4
请注意,现在条目ballxyz
不包括在内,因为它开头ball
并且不以它结束.
如果您想获得所有以ball
您开头的条目,可以简单使用
df.set_index('ids').filter(regex='^ball', axis=0)
生产
vals
ids
ballxyz 5
同样适用于列; 你需要改变的只是axis=0
部分.如果你根据列进行过滤,那就是axis=1
.