When does .str.count('\w') work and when doesn't it?

别来无恙 提交于 2020-01-06 08:16:28

问题


This is a follow up question to Regex inside findall vs regex inside count

.str.count('\w') works for me when called on the column of a dataframe, but not when called on a Series.

X_train[0:7] is a Series:

872 I'll text you when I drop x off 831 Hi mate its RV did u hav a nice hol just a mes... 1273 network operator. The service is free. For T &... 3314 FREE MESSAGE Activate your 500 FREE Text Messa... 4929 Hi, the SEXYCHAT girls are waiting for you to ... 4249 How much for an eighth? 3640 You can stop further club tones by replying \S... Name: text, dtype: object

X_train[0:7].str.count('\w') returns

872 0 831 0 1273 0 3314 0 4929 0 4249 0 3640 1 Name: text, dtype: int64)

When called on the same Series, converted into a dataframe column:

d = X_train[0:7]

df = pd.DataFrame(data=d)

df['col1'].str.count('\w') returns:

872 23 831 101 1273 50 3314 120 4929 98 4249 18 3640 98 Name: col1, dtype: int64

Why does it work on a dataframe column, but not on a series? Grateful for your advice.

来源:https://stackoverflow.com/questions/53026049/when-does-str-count-w-work-and-when-doesnt-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!