I have a pandas data frame (v 0.20.3):
df = pd.DataFrame({'coname1': ['Apple','Yahoo'], 'coname2':['Apple', 'Google']})
df['eq'] = df.apply(lambda row: row['coname1'] == row['coname2'], axis=1).astype(bool)
coname1 coname2 eq
0 Apple Apple True
1 Yahoo Google False
If I would like to replace True/False to 'Yes'/'No', I could run this:
df.replace({
True: 'Yes',
False: 'No'
})
coname1 coname2 eq
0 Apple Apple Yes
1 Yahoo Google No
Which seems to get the job done. However, if a data frame is just one row with a value of 0/1 in a column, it will be also replaced as it's being treated as Boolean.
df1 = pd.DataFrame({'coname1': [1], 'coname2':['Google'], 'coname3':[777]})
df1['eq'] = True
coname1 coname2 coname3 eq
0 1 Google 777 True
df1.replace({
True: 'Yes',
False: 'No'
})
coname1 coname2 coname3 eq
0 Yes Google 777 Yes
I would like to map True/False to Yes/No for all columns in the data frame that are of dtype bool.
How do I tell pandas to run map True/False to arbitrary strings only for the columns that are of dtype bool without explicitly specifying the names of columns as I may not know them in advance?