0

I am trying to replace values in a column, but for some reason one of the values is not changing. The three values are - 'xxx' , 'aaa' and 0. The problem is with the zero.

This is part of the code I am using:

df['source'] = np.select(conditions, ['xxx', 'aaa'], default=0) df['source'] = df['source'].replace({'xxx': 'xxx', 'aaa': 'aaa', 0: 'test'})

First I am using np.select based on some conditions and since I cant put string value on default, I want to replace the zero with another string. And I can't understand what I am doing wrong.

3
  • Just df['source'] = df['source'].replace(0, 'test') or maybe replace 0 with the string '0'? Commented Apr 9, 2020 at 14:24
  • since I cant put string value on default why is that? default = 'test' doesnot work? Commented Apr 9, 2020 at 14:25
  • @anky It says expected type int,float,complex,none Commented Apr 9, 2020 at 14:41

1 Answer 1

2

0 may be "0" as a string. check for whitespaces as well. If the series "source" is an "object" then "0" is a string.

In this case:

df['source'] = df['source'].replace({'xxx': 'xxx', 'aaa': 'aaa', '0': 'test'})
Sign up to request clarification or add additional context in comments.

1 Comment

Don't know why I did not try this before posting here.. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.