Home > Back-end >  How do I replace the a value in a pandas series with another?
How do I replace the a value in a pandas series with another?

Time:08-27

I want to replace all the values of "Red" with "Trust". How would it work because there are multiple values in each cell?

Input:

                Explanation
0  Green, Yellow, Blue, Red
1          Red, Green, Blue
2                       Red
3        Blue, Pink, Yellow

Desired Output:

                  Explanation
0  Green, Yellow, Blue, Trust
1          Trust, Green, Blue
2                       Trust
3          Blue, Pink, Yellow

I've used .replace but it only changed the cell where it's only "red" with no other colors included. Please help!

CodePudding user response:

You're looking for pd.Series.str.replace, pd.Series.replace only replaces if the whole cell matches.

df.Explanation = df.Explanation.str.replace('Red', 'Trust', regex=False)
print(df)

# Output:

                  Explanation
0  Green, Yellow, Blue, Trust
1          Trust, Green, Blue
2                       Trust
3          Blue, Pink, Yellow

CodePudding user response:

You can try use replace with dict and regex=True

df = df.replace({'Explanation' :{'Red' : 'Trust'}},regex=True)

CodePudding user response:

You can replace the substring in a column of pandas DataFrame as following methods.
  • To replace single substring :

    df = df.replace('Red','Trust', regex=True)

  • To replace multiple column substring :

    df = df.replace({'Explanation1': 'Red', 'Explanation2': 'Pink'}, {'Explanation1': 'Red', 'Explanation2': 'Hope'}, regex=True)

  • Replace using lambda function :

    df = df.apply(lambda x: x.replace('Red':'Red',regex=True))

Hope this helped you. Thank you.

  • Related