我如何更新 pandas 数据框,只需将原始数据保留在左上角? [英] how can i update pandas dataframe just keep original one at left upper corner?

查看:81
本文介绍了我如何更新 pandas 数据框,只需将原始数据保留在左上角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的情况,例如一个原始的熊猫数据框,例如:

I have a situation like that, one original pandas dataframe, for example, like:

columnA     columnB             
1           2   
1           3   

然后由于更新,该表如下所示:

then because of updating, this table looks like this:

columnA     columnB   columnC       
2           3         2
2           4         3
1           3         3

但是,我想保留原始表,所以下面显示了我想要的内容,仅更新了新内容,第三列和第三行

However, I want keep original table, so what I wanted is shown below, only new things, third column and third row updated

columnA     columnB   columnC       
1           2         2
1           3         3
1           3         3

那么我应该使用哪种合并方式来用新的行和列来扩展原始表? THX!

so which kind of merge should I use to expand original table with new rows and columns? THX!

推荐答案

我认为需要

I think need combine_first which convert integer columns to floats, so added astype:

df = df1.combine_first(df2).astype(int)
print (df)
   columnA  columnB  columnC
0        1        2        2
1        1        3        3
2        1        3        3

这篇关于我如何更新 pandas 数据框,只需将原始数据保留在左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆