Reputation: 59
I have a pandas dataframe that contains a column populated by 3 object values: "true","false", and "---"
I want to replace the "true" values with 0, and the "false" values with 1, so I wrote the following
df['True/False'] = df['True/False'].str.replace('False' '1')
df['True/False'] = df['True/False'].str.replace('True', '0')
So, I would expect input ['False','True','False','False','False','---']
to return [1,0,1,1,1,'---']
when I print(df['True/False'])
.
But instead I get:
["NaN","NaN","NaN","NaN","NaN","NaN"]
A little help?
Upvotes: 1
Views: 198
Reputation: 34086
Instead of df.replace
, you should use Series.map
as it is much faster:
Create a dict
of values you want to replace:
In [3590]: d = {'True': 0, 'False': 1}
Consider df
:
In [3592]: df = pd.DataFrame(['False','True','False','False','False','---'], columns=['A'])
In [3593]: df
Out[3593]:
A
0 False
1 True
2 False
3 False
4 False
5 ---
In [3597]: df.A = df.A.map(d).fillna(df.A)
In [3598]: df
Out[3598]:
A
0 1
1 0
2 1
3 1
4 1
5 ---
Upvotes: 3