使用字典替换列值 [英] Replace column values using a dictionary
本文介绍了使用字典替换列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个数据框,预计性别将是男性或女性.
I have this dataframe where gender is expected to be male or female.
from io import StringIO
import pandas as pd
audit_trail = StringIO('''
course_id AcademicYear_to months TotalFee Gender
260 2017 24 100 male
260 2018 12 140 male
274 2016 36 300 mail
274 2017 24 340 female
274 2018 12 200 animal
285 2017 24 300 bird
285 2018 12 200 maela
''')
df11 = pd.read_csv(audit_trail, sep=" " )
我可以使用字典来纠正拼写错误.
I can correct the spelling mistakes using dictionary.
corrections={'mail':'male', 'mael':'male', 'maae':'male'}
df11.Gender.replace(corrections)
但我正在寻找一种方法,仅保留男性/女性和其他"类别的其余选项.预期输出:
But I am looking for a way to keep only male / female and "other" category for rest of the options. Expected output:
0 male
1 male
2 male
3 female
4 other
5 other
6 male
Name: Gender, dtype: object
推荐答案
向您的corrections
字典添加另外两个虚拟条目:
Add another two dummy entries to your corrections
dict:
corrections = {'male' : 'male', # dummy entry for male
'female' : 'female', # dummy entry for female
'mail' : 'male',
'maela' : 'male',
'maae' : 'male'}
现在,使用map
和fillna
:
df11.Gender = df11.Gender.map(corrections).fillna('other')
df11
course_id AcademicYear_to months TotalFee Gender
0 260 2017 24 100 male
1 260 2018 12 140 male
2 274 2016 36 300 male
3 274 2017 24 340 female
4 274 2018 12 200 other
5 285 2017 24 300 other
6 285 2018 12 200 male
这篇关于使用字典替换列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文