Python 索引生成器 将熊猫作为pd导入 df=pd.DataFrame({'user':['Bob','Jane','Alice'], “收入”:[40000,50000,42000]})

Python 索引生成器 将熊猫作为pd导入 df=pd.DataFrame({'user':['Bob','Jane','Alice'], “收入”:[40000,50000,42000]}),python,pandas,dataframe,indexing,Python,Pandas,Dataframe,Indexing,我有一个过滤器列表: f=['A','B','C'] 并希望获得所有数据,如: df[df.user.str.startswith(f)] 当然,这是行不通的,我在这里为搜索引擎写了一个错误,也许它会帮助一些人 >>> TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported type

我有一个过滤器列表:

f=['A','B','C']
并希望获得所有数据,如:

df[df.user.str.startswith(f)]
当然,这是行不通的,我在这里为搜索引擎写了一个错误,也许它会帮助一些人

    >>> TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
使用:

输出

    user  income
0    Bob   40000
2  Alice   42000
从match的文档中:

确定每个字符串是否以正则表达式的匹配项开头

使用:

输出

    user  income
0    Bob   40000
2  Alice   42000
从match的文档中:

确定每个字符串是否以正则表达式的匹配项开头


你需要传递一个元组

从:

str.startswith(前缀[,开始[,结束]])

如果字符串以前缀开头,则返回
True
,否则返回
False
前缀
也可以是要查找的前缀元组。

输出:

    user  income
0    Bob   40000
2  Alice   42000

你需要传递一个元组

从:

str.startswith(前缀[,开始[,结束]])

如果字符串以前缀开头,则返回
True
,否则返回
False
前缀
也可以是要查找的前缀元组。

输出:

    user  income
0    Bob   40000
2  Alice   42000

谢谢你的回答。我接受你的回答,因为它更一般。谢谢你的回答。我接受你的回答,因为它更一般。谢谢!很抱歉“不接受”你的答案,我真的很喜欢你的解决方案!非常感谢。很抱歉“不接受”你的答案,我真的很喜欢你的解决方案!