Python 根据索引合并两个系列
在谷歌搜索了很长一段时间后,我的问题(可能经常被问到)没有找到解决方案 我有两个数据帧:Python 根据索引合并两个系列,python,pandas,Python,Pandas,在谷歌搜索了很长一段时间后,我的问题(可能经常被问到)没有找到解决方案 我有两个数据帧: DF1: DF2: val val index index 1 3 2 5 3 10 4 15 5 20 7 35 6 30 8 40
DF1: DF2:
val val
index index
1 3 2 5
3 10 4 15
5 20 7 35
6 30 8 40
需要这样的输出:
DF_out:
val
index
1 3
2 5
3 10
4 15
5 20
6 30
7 35
8 40
DF1和DF2应根据其他指标进行组合和排序。
旁注:
- DF1和DF2从来不会有两次相同的索引
- 数据帧的值总是续集
pd.concat((df1,df2)).sort_index()
是您所需要的,或者您也可以使用combine_first
df1.combine_first(df2)
:related:and[](pd.concat((df1,df2)).sort_index()
是您所需要的,或者您也可以使用combine_first
df1.combined_firstdf1
df = pd.concat([DF1, DF2]).sort_index()
print (df)
val
index
1 3
2 5
3 10
4 15
5 20
6 30
7 35
8 40