在pandas/python中创建一个唯一的指示符two join两个数据集
如何在pandas/python中组合数据帧中的四列以创建唯一的指示符并进行左连接 这是我想要实现的最好的方式吗在pandas/python中创建一个唯一的指示符two join两个数据集,python,pandas,Python,Pandas,如何在pandas/python中组合数据帧中的四列以创建唯一的指示符并进行左连接 这是我想要实现的最好的方式吗 example: make a unique indicator (col5) then setup a join with another dataframe using the same logic col1 col2 col3 col4 col5 apple pear mango tea applepearmango
example: make a unique indicator (col5)
then setup a join with another dataframe using the same logic
col1 col2 col3 col4 col5
apple pear mango tea applepearmangotea
然后做一个连接,比如
pd.merge(df1, df2, how='left', on='col5')
无论是4列还是2列,此问题都是相同的。您不需要创建唯一的组合键。您只需在多个列上合并 考虑两个数据帧
d1
和d2
。它们共用两列
d1 = pd.DataFrame([
[0, 0, 'a', 'b'],
[0, 1, 'c', 'd'],
[1, 0, 'e', 'f'],
[1, 1, 'g', 'h']
], columns=list('ABCD'))
d2 = pd.DataFrame([
[0, 0, 'a', 'b'],
[0, 1, 'c', 'd'],
[1, 0, 'e', 'f'],
[2, 0, 'g', 'h']
], columns=list('ABEF'))
d1
A B C D
0 0 0 a b
1 0 1 c d
2 1 0 e f
3 1 1 g h
d2
A B E F
0 0 0 a b
1 0 1 c d
2 1 0 e f
3 2 0 g h
我们可以使用
我们可以显式地使用列
d1.merge(d2, 'left', on=['A', 'B'])
A B C D E F
0 0 0 a b a b
1 0 1 c d c d
2 1 0 e f e f
3 1 1 g h NaN NaN
你想完成什么?你的问题需要更多的信息;输入和期望输出的示例是什么?
d1.merge(d2, 'left', on=['A', 'B'])
A B C D E F
0 0 0 a b a b
1 0 1 c d c d
2 1 0 e f e f
3 1 1 g h NaN NaN