Replacing column values in a pandas DataFrame
Solution 1:
If I understand right, you want something like this:
w['female'] = w['female'].map({'female': 1, 'male': 0})
(Here I convert the values to numbers instead of strings containing numbers. You can convert them to "1"
and "0"
, if you really want, but I'm not sure why you'd want that.)
The reason your code doesn't work is because using ['female']
on a column (the second 'female'
in your w['female']['female']
) doesn't mean "select rows where the value is 'female'". It means to select rows where the index is 'female', of which there may not be any in your DataFrame.
Solution 2:
You can edit a subset of a dataframe by using loc:
df.loc[<row selection>, <column selection>]
In this case:
w.loc[w.female != 'female', 'female'] = 0
w.loc[w.female == 'female', 'female'] = 1
Solution 3:
w.female.replace(to_replace=dict(female=1, male=0), inplace=True)
See pandas.DataFrame.replace() docs.
Solution 4:
Slight variation:
w.female.replace(['male', 'female'], [1, 0], inplace=True)
Solution 5:
This should also work:
w.female[w.female == 'female'] = 1
w.female[w.female == 'male'] = 0