本文介绍了我如何更新 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!

推荐答案

我认为需要 combine_first 会将integer列转换为float,因此添加了 astype :

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 数据框,只需将原始数据保留在左上角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 02:56
查看更多