我尝试将多个新的dataFrames合并到一个主框架中。假设主数据框:
key1 key2
0 0.365803 0.259112
1 0.086869 0.589834
2 0.269619 0.183644
3 0.755826 0.045187
4 0.204009 0.669371
我尝试将以下两个数据集合并到主要数据集“
新数据1 ”中:
key1 key2 new feature
0 0.365803 0.259112 info1
新数据2:
key1 key2 new feature
0 0.204009 0.669371 info2
预期结果:
key1 key2 new feature
0 0.365803 0.259112 info1
1 0.776945 0.780978 NaN
2 0.275891 0.114998 NaN
3 0.667057 0.373029 NaN
4 0.204009 0.669371 info2
我试过的
test = test.merge(data1, left_on=['key1', 'key2'], right_on=['key1', 'key2'], how='left')
test = test.merge(data2, left_on=['key1', 'key2'], right_on=['key1', 'key2'], how='left')
第一个效果很好,但第二个效果不好,我得到的结果是:
key1 key2 new feature_x new feature_y
0 0.365803 0.259112 info1 NaN
1 0.776945 0.780978 NaN NaN
2 0.275891 0.114998 NaN NaN
3 0.667057 0.373029 NaN NaN
4 0.204009 0.669371 NaN info2
谢谢你的帮助!
aluckdog
胡说叔叔
哈士奇WWW
相关分类