Replacing column values in a pandas DataFrame

I'm trying to replace the values in one column of a dataframe. The column ('female') only contains the values 'female' and 'male'.

I have tried the following:


But receive the exact same copy of the previous results.

I would ideally like to get some output which resembles the following loop element-wise.

    if w['female'] =='female':
        w['female'] = '1';
        w['female'] = '0';

I've looked through the gotchas documentation ( but cannot figure out why nothing happens.

Any help will be appreciated.

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.