pandas中的replace用法
replace既可以替换某列,也可以替换某行
replace(self, to_replace=None, value=None, inplace=False, limit=None, regex=False, method=‘pad‘, axis=None)
传入的参数既可以是列表,也可以是字典,但是传入的字典,key和value必须不能重复(严格),否则报错
ValueError: Replacement not allowed with overlapping keys and values
例如传入如下字典是有问题的
mapping_dict = {"prodcode_type":{ "1":"0", "2":"3", "3":"1", "4":"1", "5":"1", "6":"2", "7":"b", "8":"z", "9":"2", "a":"5", "b":"1", "d":"4", "t":"e", "u":"l" }} mfbaseinfo = mfbaseinfo.replace(mapping_dict)
此时如果我们确实有这样的需求,可以把字典改成列表,就可解决。
mfbaseinfo[‘prodcode_type‘].replace([‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘a‘,‘b‘,‘d‘,‘t‘,‘u‘], [‘0‘,‘3‘,‘1‘,‘1‘,‘1‘,‘2‘,‘b‘,‘z‘,‘2‘,‘5‘,‘1‘,‘4‘,‘e‘,‘l‘],inplace=True)
相关推荐
满地星辰 2020-09-16
shqhope 2020-06-23
WebVincent 2020-06-16
lynjay 2020-06-14
ajaxtony 2020-06-11
有梦就能实现 2020-06-05
zhendeshifeng 2020-05-27
gcgl000hugang 2020-02-10
Yasin 2020-05-05
Darklovy 2020-04-16
hellobabygogo 2020-03-06
程松 2020-02-23
wbingyang 2020-02-16
文山羊 2020-02-14
zehuawong 2020-02-11
ELEMENTS爱乐冬雨 2019-12-29