You can use mask to replace values ββwith boolean mask - the advantage of this solution is there is no original types :
df = pd.DataFrame({'Col':[1, False, 0]}) df['Col'] = df['Col'].mask(df['Col'].astype(str) == '0', 2).replace(False, 3) print (df) Col 0 1 1 3 2 2
The solution is with a Series.replace dict , but first converting to str on astype works, but usually it converts all the values ββto str , which can be a problem with real data.
d = {'0':'Zero', 'False':False} df = df['Col'].astype(str).replace(d) print (df) 0 1 1 False 2 Zero Name: Col, dtype: object
I will try to create a more general solution with map and check bools on isinstance :
df = pd.DataFrame({'Col':[1, False, 0, True,5]}) print (df) Col 0 1 1 False 2 0 3 True 4 5 m = df['Col'].apply(lambda x: isinstance(x, bool)) df['Col'] = df['Col'].mask(m, df['Col'].map({False:2, True:3})) print (df) Col 0 1 1 2 2 0 3 3 4 5
source share