2

Categorical Variable : Gender | dtype: Object | Values: Male, male, m, M, Female, female, f, F |

I want to replace all values to 'Male' & 'Female' accoridingly. Replace is not working, showing that 'Male' and 'Female' does not exist. I can replace them by 1 & 0 but I don't want to make it an ordinal variable

This is my code:-

bck = pd.read_csv('BCK.csv')
bck['Gender'].value_counts()
bck.Gender.dtype
bck['Gender'] = bck['Gender'].astype('str')
bck.Gender.dtype
bck.Gender.replace(to_replace=dict(m = Male, f = Female, male =  Male,     female =  Female, M = Male, F  = Female), inplace=True)

This is not working and displaying, " Male does not exist".

Deepak Lunawat
  • 33
  • 1
  • 1
  • 4
  • 4
    `Replace is not working, showing that 'Male' and 'Female' does not exist.` Please supply a **[mcve]** demonstrating this behaviour. See [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) if you need help with this. – jpp Dec 20 '18 at 10:08

2 Answers2

5

Try:

df['Gender'].str[0].str.upper().map({'M':'Male', 'F':'Female'})

Explanation

This chains two Series.str accessor methods and Series.map to:

  1. Return the first character of each Gender value by indexing at [0]
    eg. 'male'[0] = m and 'Female'[0] = 'F'

  2. str.upper() to upper case all of these values

  3. Finally, .map to map 'M' to 'Male' and 'F' to 'Female'

Chris Adams
  • 18,389
  • 4
  • 22
  • 39
2

you're dictionary is not correct. You also have them as variables which should be saying name 'Male' is not defined, not that they don't exist. They need to be strings.

Try:

bck.Gender.replace({'m':'Male', 'f':'Female', 'male':'Male','female':'Female', 'M':'Male', 'F':'Female'}), inplace=True)

or can use .map function:

x = {'m':'Male', 'f':'Female', 'male':'Male','female':'Female', 'M':'Male', 'F':'Female'}

bck['Gender'] = bck['Gender'].map(x)
chitown88
  • 27,527
  • 4
  • 30
  • 59