A B
t f
t t
f f
t t
How to convert this kind of data in numeric form in python for multiple columns?
CodePudding user response:
You could replace
the values using a dictionary:
df[['A', 'B']] = df[['A', 'B']].replace({ 't': 1, 'f': 0 })
Output:
A B
0 1 0
1 1 1
2 0 0
3 1 1
CodePudding user response:
Use factorize
with DataFrame.stack
for numeric columns by categories:
cols = ['A','B']
y, label = pd.factorize(df[cols].stack())
df[cols] = y.reshape(df[cols].shape)
print (df)
A B
0 0 1
1 0 0
2 1 1
3 0 0
Dictionary for mapping is possible generate by:
d = dict(zip(label, y))
print (d)
{'t': 0, 'f': 1}
CodePudding user response:
the first thing i came to was using factorize, but as it's already given above i came to the second:
df = df.stack().to_frame().apply(lambda x: pd.Categorical(x).codes).unstack().droplevel(0,1)
>>> df
'''
A B
0 1 0
1 1 1
2 0 0
3 1 1