我正在尝试在Python Pandas中使用df['column_name'].str.count("+")
,但是我收到了错误信息:
"error: nothing to repeat"
用普通字符使用这个方法是可行的,例如df['column_name'].str.count("a")
可以正常工作。
同时,使用 "^" 符号也会出现问题。如果我使用df['column_name'].str.contains("^")
,结果会不正确 - 看起来 "^" 被解释为 " "(空格)。
令人惊讶的是,如果我在常规的非Pandas字符串上使用.count("+")
和.contains("^")
,它们可以完美地工作。
以下是简单的工作示例:
df = pd.DataFrame({'column1': ['Nighthawks+', 'Dragoons'], 'column2': ['1st', '2nd']}, columns = ['column1', 'column2'])
应用
df["column1"].str.contains("^")
将得到"True, True",但应该是"False, False"。而应用
df["column1"].str.count("+")
会出现错误:
但是,在panda之外,"error: nothing to repeat"
"bla++".count("+")
可以正确地给出结果"2"。有什么解决方法吗?谢谢。