[英]Python pandas .map saves only last edit
I am trying to use pandas .map to edit a dataset as in the following code: 我正在尝试使用pandas .map编辑数据集,如以下代码所示:
df['Region'] = df['Region'].astype('category')
reg = df['Region']
cats = reg.cat.categories
ncats = len(cats)
n = len(os)
north = (...)
south = (...)
center = (...)
islands = (...)
d1 = {cats[i]:'South' for i in range(ncats) if cats[i] in south}
d2 = {cats[i]:'North' for i in range(ncats) if cats[i] in north}
d3 = {cats[i]:'Center' for i in range(ncats) if cats[i] in center}
d4 = {cats[i]:'Islands' for i in range(ncats) if cats[i] in islands}
df['Reg_cat'] = df['Region'].map(d1)
df['Reg_cat'] = df['Region'].map(d2)
df['Reg_cat'] = df['Region'].map(d3)
df['Reg_cat'] = df['Region'].map(d4)
df['Reg_cat'] = df['Reg_cat'].astype('category')
df['Reg_cat'].cat.categories
df['Reg_cat']
The code does work but it only applies the last .map request. 该代码可以工作,但是仅应用最后一个.map请求。 So in this case it applies d4.
因此,在这种情况下,它适用d4。 If d1 is the last one it applies that one.
如果d1是最后一个,则应用那个。 What am I doing wrong?
我究竟做错了什么?
Each successive map
call replaces everything not inside the mapper with NaN. 每次后续的
map
调用都会用NaN替换不在映射器内部的所有内容。
Try building a single dictionary and passing that instead. 尝试建立一个字典并传递它。
m = {'North' : north, 'South' : south, 'Center' : center, 'Islands', islands}
d = {v2 : k for k, v in m.items() for v2 in v}
df['Reg_cat'] = df['Reg_cat'].map(d)
Note: 注意:
reg
reg
cats
cats
ncats
ncats
n
, whatever that is n
,不管它是什么 Everytime you are calling df['Reg_cat'] = df['Region'].map(d#)
you are overwriting the value of df['Reg_cat']
. 每次调用
df['Reg_cat'] = df['Region'].map(d#)
都会覆盖df['Reg_cat']
。 If you'd like to keep all the values, consider adding them as separate columns. 如果要保留所有值,请考虑将它们添加为单独的列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.