Converting all occurrence of True/False to 1/0 in a dataframe with mixed datatype

applymap is not in-place by default, it will return a new dataframe.

The correct way:

test = test.applymap(lambda x: 1 if x == True else x)
test = test.applymap(lambda x: 0 if x == False else x)

or

test = test.applymap(lambda x: 1 if x == True else x).test.applymap(lambda x: 0 if x=='False' else x)

or simply

test.applymap(lambda x: 1 if x == True else x, inplace=True)
test.applymap(lambda x: 0 if x == False else x, inplace=True)


Although replace seems the best way of achieving this:

test.replace(False, 0, inplace=True)

For a single column, the simplest way by far is to convert the column type. Pandas is smart enough to map boolean to int correctly.

df.column_name = df.column_name.astype(int)

If df.column_name starts as Boolean, it will become zeros and ones after converting to type int