.str.count('\ w')什么时候起作用,什么时候不起作用?

时间:2018-10-27 20:33:40

标签: python pandas count

这是对Regex inside findall vs regex inside count

的跟进问题

.str.count('\w')在数据框的列上调用时对我有用,而在Series上调用时不起作用。

X_train[0:7]是一个系列:

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') 返回

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

在同一系列上调用时,转换为数据框列:

d = X_train[0:7]

df = pd.DataFrame(data=d)

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

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

为什么它不能用于数据框列,而不能用于系列?感谢您的建议。

0 个答案:

没有答案