在c列映射后, 如果A列有值,则插入A列的值;如果没有,则插入B列
data1 data2
a b c a c d
a1 b1 c1 1a c1 1d
b2 c2 2a c2 2d
a3 c3 3a c3 3d
4a c4 4d
我想要的结果
^{pr2}$我尝试了以下方法,但并不满意。在
->>> result = data1.merge(data2, on=['c'])
Prefixes _x and _y are created. combine_first is not applied.
->>> result = data1.combine_first(data2)
It is not mapped by column c.
我怎样才能得到好的结果? 我请求你的帮助。 谢谢你
使用@IdoS安装程序:
您可以使用
^{pr2}$set_index
,combine_first
,然后重新编制索引:输出:
你也可以试试这个方法:
^{pr2}$
我不是百分之百的清楚你如何索引你的数据帧(
data1
和data2
),但是如果你在列'c'
上对它们进行索引,那就可以了。在我是这样创建你的数据的:
然后我将两者的索引设置为列
^{pr2}$'c'
:然后我像您一样使用
combine_first
:我明白了:
不知道为什么不需要索引为
'c4'
的行或列'd'
,但删除它们很容易:然后我重新排序以得到你想要的结果:
相关问题 更多 >
编程相关推荐