无法替换熊猫中的值

时间:2020-01-27 00:47:42

标签: python pandas

我是一个数据列,其中的列具有int和str值的组合,需要将其值替换为0。我曾尝试使用replace来做到这一点,但是由于某些原因,它将无法正常工作。

我可以在导入csv之前简单地使用excel中的“查找和替换”功能来执行此操作,但是为什么当功能可以简单地执行任务而无需添加额外的工作时执行此操作。我已经提到了下面的帖子,但是仍然没有任何效果。

Pandas replacing elements not working

下面是我使用的代码

crew_hours1 = pd.Series(crew_data.loc[:, 'Crew/Equip 1 Hours'])
crew_hours1.replace('<Not-Defined>', '0')

当我打印结果时,返回以下输出:

0                  4
1                  4
2      <Not Defined>
3                  6
4                  2
       ...      
134    <Not Defined>
135    <Not Defined>
136    <Not Defined>
137    <Not Defined>
138    <Not Defined>

由于某种原因,它将不起作用。我已经使用了inplace = True和regex = True,但仍然无法正常工作。 有人对此有解决方案吗?

2 个答案:

答案 0 :(得分:2)

问题是文本显示<Not defined>,而您在公式中使用<Not-defined>。这应该起作用:

crew_hours1 = crew.hours1.replace('<Not Defined>',0,regex=True)

答案 1 :(得分:0)

尝试C:\Users\Turtle\AppData\Local\Programs\Python\Python38\Lib\site-packages\selenium-4.0.0a3-py3.8.egg\selenium\webdriver\chromium

series.str.replace()