[英]Wrong column mapping of pandas DataFrame from dictionary
I am trying to map 2 DataFrames我正在尝试 map 2 DataFrames
L1
ship city code
NaN aa 12
NaN bb 23
NaN cc 13
NaN dd 43
B1
ship city
21 dd
32 bb
43 aa
654 cc
34 bb
54 aa
I want to map column code
from L1
to B1
.我想 map 列code
从L1
到B1
。 I tried mapping using a dictionary, so I would get a result like this我尝试使用字典进行映射,所以我会得到这样的结果
Expected_Result =
ship city code
21 dd 43
32 bb 23
43 aa 12
654 cc 13
34 bb 23
54 aa 12
code_dict = dict(zip(L1['city'],L1['code']))
B1['code'] = L1['city'].map(code_dict)
print(B1)
The result that I got is not what I expected.我得到的结果不是我所期望的。
Please help me fix this issue.请帮我解决这个问题。
ship city code
21 dd 12
32 bb 23
43 aa 13
654 cc 43
34 bb NaN
54 aa NaN
I think you're looking for:我想你正在寻找:
from pandas import DataFrame
df1 = DataFrame([
{'city': 'aa', 'code': 12},
{'city': 'bb', 'code': 23},
{'city': 'cc', 'code': 13},
{'city': 'dd', 'code': 43}
])
df2 = DataFrame([
{'ship': '21', 'city': 'dd'},
{'ship': '32', 'city': 'bb'},
{'ship': '43', 'city': 'aa'},
{'ship': '654', 'city': 'cc'},
{'ship': '34', 'city': 'bb'},
{'ship': '54', 'city': 'aa'}
])
expected_result = df2.merge(df1, on='city')
print(expected_result)
Result:结果:
ship city code
0 21 dd 43
1 32 bb 23
2 34 bb 23
3 43 aa 12
4 54 aa 12
5 654 cc 13
Two options.两种选择。
data setup:数据设置:
ship=[np.nan]*4
city=['aa','bb','cc','ddd']
code=[12,23,13,43]
ship2=[21,32,43,654,34,54]
city2=['dd','bb','aa','cc','bb','aa']
l1=pd.DataFrame({"ship":ship,"city":city,"code":code})
b1=pd.DataFrame({"ship":ship2,"city":city2})
1st through merge on city:第一次通过城市合并:
l1.merge(b1,on=['city'],how='inner')[['ship_y','city','code']].rename({"ship_y":"ship"},axis=1)
2nd use map/dictionary:第二次使用地图/字典:
ship_city_d = dict(zip(l1.city, l1.code)) # build the dictionary
b1['code']=b1['city'].apply(lambda x: str(ship_city_d.get(x,"Not Found")) )
note: b1 will have some entries if key not found, you can drop those record as next step if not needed.注意:如果找不到密钥,b1 将有一些条目,如果不需要,您可以将这些记录作为下一步删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.