[英]Map two dataframes in a case insensitive way (Python pandas)
I have two dataframe of different lenghts.我有两个不同长度的 dataframe。
db
index| Size | GROUP FORMAT
1 | AA | Unknown
2 | BB | Unknown
3 | CC | Unknown
db2
index| GROUP FORMAT| FORMAT
1 | G1 | Aa
2 | G2 | bB
The column FORMAT of db2 and Size of db have the same letters, but different upper/lower cases may happen. db2 的 FORMAT 列和 db 的 Size 的字母相同,但大小写可能不同。 I want to map them in order to get:
我想 map 他们以获得:
db
index| Size | GROUP FORMAT
1 | AA | G1
2 | BB | G2
3 | CC | Unknown
However, if possible, I'd rather not duplicate and drop any column.但是,如果可能的话,我宁愿不复制和删除任何列。 Is it possible to map the two dataframes in a case insensitive way?
是否可以以不区分大小写的方式对两个数据帧进行 map?
Try to convert all to uppercase, then merge:尝试全部转换为大写,然后合并:
df1['GROUP FORMAT' ] = (df1.merge(df2.assign(FORMAT=df2.FORMAT.str.upper()),
left_on='Size', right_on='FORMAT', how='left')
['GROUP FORMAT_y']
.fillna(df1['GROUP FORMAT'])
)
Output: Output:
index Size GROUP FORMAT
0 1 AA G1
1 2 BB G2
2 3 CC Unknown
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.