DataFrame中的.replace()不会替换

rob*_*aro -1 python pandas

我试图;从DataFrame中的列中删除,但是当我使用该.replace(';', ' ')方法时它不会替换半圆,这是我如何做到这一点:

for i in excel_file['dateTime']:
    i.replace(';', ' ')
Run Code Online (Sandbox Code Playgroud)

这就是我的'dateTime'专栏的样子:

11-06-18;
18-06-18;
18-09-18;
14-06-18;
20-06-18;
13-06-18;
21-06-18;
19-06-18;
19-06-18;
20-06-18;
11-06-18;
22-06-18;
Run Code Online (Sandbox Code Playgroud)

Joh*_*nck 6

不要使用for循环.代替:

excel_file['dateTime'] = excel_file['dateTime'].str.replace(';', ' ')
Run Code Online (Sandbox Code Playgroud)

这个答案隐含的是str.replace()返回一个新字符串,而不是原位修改原始字符串.这是原始代码中的功能缺陷.但是没有for循环就可以快几倍.

参考:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.replace.html