【发布时间】:2016-11-30 21:33:01
【问题描述】:
考虑以下数据框。我想计算出现在字符串中的“$”的数量。我在 pandas (http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.count.html) 中使用了str.count 函数。
>>> import pandas as pd
>>> df = pd.DataFrame(['$$a', '$$b', '$c'], columns=['A'])
>>> df['A'].str.count('$')
0 1
1 1
2 1
Name: A, dtype: int64
我期待结果是[2,2,1]。我究竟做错了什么?
在 Python 中,字符串模块中的count 函数返回正确的结果。
>>> a = "$$$$abcd"
>>> a.count('$')
4
>>> a = '$abcd$dsf$'
>>> a.count('$')
3
【问题讨论】:
-
我什至不知道
str.count...谢谢!