Home > database >  How to change values of a column (boolean) by other values?
How to change values of a column (boolean) by other values?

Time:12-12

I would like to change the boolean values of my "success" column in my "members" dataframe. Specifically, I would like to change the True values to "success" and the False values to "fail". Would you know how to do this?

code :

import pandas as pd 
members = pd.read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-09-22/members.csv")

CodePudding user response:

Using replace as follow:

members['success'].replace({True: 'success', False: "fail"}, inplace = True)

Remember, inplace applies the changes directly to the DataFrame

CodePudding user response:

This can be easily done by using pandas replace(). Careful it is different from python's built-in replace .str.replace().

members['success'].replace({False:"fail",True:"success"},inplace=True)

Based on Corralien's comment and this answer addressing the use of inplace, perhaps it would be better to redefine the series:

members['success'] = members['success'].replace({False:"fail",True:"success"})
  • Related