Replacing column values in a pandas DataFrame

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.

Leave a Comment