首页 > 解决方案 > 如何添加两个不同维度的数据框?

问题描述

我在 Python 中有两个数据框,需要添加,但希望输出作为最后一个表,而不是作为执行结果的表df1+df2

df1:

index   6   12  24
2010    2   3   4
2011    2   3   4
2012    2   3   4

df2:

index   6   12
2011    2   3   
2012    2   3

df1+df2:

index   6   12  
2011    4   6   
2012    4   6

所需输出:

index   6   12  24   
2010    2   3   4    
2011    4   6   4    
2012    4   6   4

标签: pythonpandasdataframe

解决方案


是的,使用:

df1.add(df2, fill_value=0)

推荐阅读