Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在表数据框中放置模式_Regex_Python 3.x - Fatal编程技术网

Regex 如何在表数据框中放置模式

Regex 如何在表数据框中放置模式,regex,python-3.x,Regex,Python 3.x,我在数据框中有一个日期列,日期类似于“2020-03-02”或“2020-02” 我想删除像这样的日期的“-03-”。 我尝试: 但是返回一个类似于带有“0”的新数据帧的 如何删除它?您可以将列转换为字符串类型,然后运行正则表达式替换: df['Collection_Date']=df['Collection_Date'].astype('str').str.replace(r'-\d\d-','') # ^^^

我在数据框中有一个日期列,日期类似于“2020-03-02”或“2020-02”

我想删除像这样的日期的“-03-”。 我尝试:

但是返回一个类似于带有“0”的新数据帧的


如何删除它?

您可以将列转换为字符串类型,然后运行正则表达式替换:

df['Collection_Date']=df['Collection_Date'].astype('str').str.replace(r'-\d\d-','')
#                                           ^^^^^^^^^^^^^ 

您可以将列转换为字符串类型,然后运行正则表达式替换:

df['Collection_Date']=df['Collection_Date'].astype('str').str.replace(r'-\d\d-','')
#                                           ^^^^^^^^^^^^^ 

请尝试
df['Collection\u Date']=df['Collection\u Date'].astype('str').str.replace(r'-\d\d-,'')
这就是wokrs!!!非常感谢。很高兴它对你有用。请考虑通过点击来接受答案。✓ 在左边(请参见)。尝试
df['Collection\u Date']=df['Collection\u Date']].astype('str').str.replace(r'-\d\d-,'')
这就是wokrs!!!非常感谢。很高兴它对你有用。请考虑通过点击来接受答案。✓ 在左边(参见)。