0

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")

2 Answers2

1

Using replace as follow:

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

Remember, inplace applies the changes directly to the DataFrame

0

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"})
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • I prefer this version: `members = members.replace({'success': {False:"fail",True:"success"}})`. I don't like the argument `inplace=True` :) – Corralien Dec 11 '21 at 15:24
  • Thank you Corralien, I will update my answer to show this, because you are right, it is better to avoid the use of inplace. – Celius Stingher Dec 11 '21 at 15:27