jeudi 7 mai 2020

Conditionally replacing integers with strings in singular Pandas df column? [duplicate]

Looking for some help replacing a column of age stored as integers with "over 30" and "under 30" based on whether the integers are over or under 30.

This is the data frame question_one as shown when I called head() on it

ID  age education   marital_status  occupation  annual_income
0   1   39  bachelors   never married   professional    high
1   2   50  doctorate   married professional    mid
2   3   18  high school never married   agriculture low
3   4   30  bachelors   married professional    mid
4   5   37  high school married agriculture mid

This is the method I've tried, which gives me an error saying series are mutable and therefore cannot be hashed, which I don't really understand if I'm honest.

if (question_one['age'] < 30).any():
    question_one[question_one['age'] > 30, 'age'] = 'Over 30'
else:
    question_one[question_one['age'] < 31, 'age'] = '30 and under'

I have a feeling I'm missing something super simple

Any help would be appreciated, sorry for the super basic question!

Aucun commentaire:

Enregistrer un commentaire