0

I a dataframe with columns that have a combination of int and str value in which I need to replace the value to 0. I have tried using replace to do this, but for some reason it will not work.

I could simply do this using the Find and Replace function in excel prior to importing the csv, but why do this when a funtion could simply perform the task without the added extra work. I have referred to the post below, but still nothing works.

Pandas replacing elements not working

below is the code that I have used

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

When I print the result the following output is returned:

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>

For some reason it will not work. I have used both inplace = True and regex = True, but still fails to work. Does anyone have a solution for this?

jasw
  • 33
  • 2
  • 7

2 Answers2

2

Well the problem is that the text shows <Not defined> and you are using <Not-defined> in your formula. This should work:

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

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
1

try series.str.replace()

crew_hours1 = crew_hours1.str.replace('<Not-Defined>', '0')
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143