我有以下Pandas DataFrame:
>>> sample_dataframe
P
0 107.35
1 99.35
2 75.85
3 92.34
当我尝试以下操作时,输出如下:
>>> sample_dataframe[sample_dataframe['P'].astype(str).str.count('.') == 1]
Empty DataFrame
Columns: [P]
Index: []
使用正则表达式转义字符会发生以下情况:
>>> sample_dataframe[sample_dataframe['P'].astype(str).str.count('\.') == 1]
P
0 107.35
1 99.35
2 75.85
3 92.34
以下内容进一步强化了这一点:
>>> sample_dataframe['P'].astype(str).str.count('.')
0 6
1 5
2 5
3 5
Name: P,dtype: int64
vs。
sample_dataframe['P'].astype(str).str.count('\.')
0 1
1 1
2 1
3 1
Name: P,dtype: int64
因此,.
表达式实际上将所有字符都计为正则表达式通配符,减去换行符,因此计数6、5、5、5与转义的\.
相比,后者仅计数实际字符.
的出现。
但是,从字符串本身调用的常规函数的行为似乎有所不同,并且不需要'。'的正则表达式转义:
>>> '105.35'.count('.')
1
>>> '105.35'.count('\.')
0
编辑:基于一些答案,我将尝试阐明下面的类函数调用(而上面的是实例化对象的方法调用):
>>> str.count('105.35','.')
1
>>> str.count('105.35','\.')
0
我不确定在后台使用CPython的与Pandas相关的方法(由于NumPy操作)是否将其实现为正则表达式(包括df.apply),或者是否与{{1}中的差异有关}类函数str
(即count
)与实例化对象的str.count()
类方法(在上面的示例str
中)'105.35'
(即{{ 1}})。类与对象函数/方法之间的差异是根本原因(以及它们的实现方式),还是由通过NumPy实现DataFrames引起的?
我真的想了解更多有关此的信息,以真正了解其工作原理