python - Replacing column values in a pandas DataFrame

ID : 20326

viewed : 13

Tags : pythonpandaspython

Top 5 Answer for python - Replacing column values in a pandas DataFrame

vote vote

99

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.

vote vote

90

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 
vote vote

70

w.female.replace(to_replace=dict(female=1, male=0), inplace=True) 

See pandas.DataFrame.replace() docs.

vote vote

68

Slight variation:

w.female.replace(['male', 'female'], [1, 0], inplace=True) 
vote vote

53

This should also work:

w.female[w.female == 'female'] = 1  w.female[w.female == 'male']   = 0 

Top 3 video Explaining python - Replacing column values in a pandas DataFrame

Related QUESTION?