pandas 行选择字符串从列表中任何项目开始的位置 [英] Pandas Row Select Where String Starts With Any Item In List
本文介绍了 pandas 行选择字符串从列表中任何项目开始的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基于特定的字符串列在pandas数据框中选择行的子集,其中值以列表中任意数量的值开头.
I want to select a subset of rows in a pandas dataframe, based on a particular string column, where the value starts with any number of values in a list.
此版本的一个小版本:
df = pd.DataFrame({'a': ['aa10', 'aa11', 'bb13', 'cc14']})
valids = ['aa', 'bb']
因此,在这种情况下,我只希望a
以aa
或bb
开头的行.
So I want just those rows where a
starts with aa
or bb
in this case.
推荐答案
您需要startswith
df.a.str.startswith(tuple(valids))
Out[191]:
0 True
1 True
2 True
3 False
Name: a, dtype: bool
使用原始df过滤后
df[df.a.str.startswith(tuple(valids))]
Out[192]:
a
0 aa10
1 aa11
2 bb13
这篇关于 pandas 行选择字符串从列表中任何项目开始的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文