The data looks like the below
Code Col1 Col2 col3 col4 col5 col6 Col7 Col8
0 A nan nan False c 12 True 8
1 B 1 Nan True ab 1 True 9
2 C Nan 5 True rr 89.7 False 78.8
3 D 34.4 0 True tr 123 True 90.3
I have more than 1 column having true/false data, the column will not always be in the same location and their number could defer, i want to replace the True anf False text with 1 or 0 numbers.
import pandas as pd
import datetime
# Create a dataframe
df = pd.read_excel(r'path.xls', sheet_name='mydata')
#print(df)
def foo_bar(x):
if x == 'True':
return x.replace('True', '1')
if x == 'False':
return x.replace('False', '0')
df.applymap(foo_bar)
for v in df['col 7']:
print(v)
The print is showing :
True
True
False
True
1.0
the values are not being changed to True and False as expected