python - Joining two panda dataframes with same columns and merging rows with same index
问题描述
I have two dataframes df1 and df2 each with the same column names using timestamps as indicies. I want to concatenate the two dataframes whilst merging rows with the same index choosing the values stored in df2 as preference. This is poorly worded but see below. E.g
>>> df1= TimeStamp A_Output B_Output C_Output
00:00:00 20 15 5
00:00:06 20 NaN 3
00:00:15 15 6 NaN
00:00:20 20 NaN 5
00:00:30 25 14 10
>>> df2= TimeStamp A_Output B_Output C_Output
00:00:00 15 5 8
00:00:04 16 NaN NaN
00:00:06 17 NaN NaN
00:00:15 NaN NaN 2
00:00:18 19 NaN NaN
00:00:21 14 NaN NaN
00:00:26 32 NaN 5
>>> df3= TimeStamp A_Output B_Output C_Output
00:00:00 15 5 8
00:00:04 16 NaN NaN
00:00:06 17 NaN 3
00:00:15 15 6 2
00:00:18 19 NaN NaN
00:00:21 14 NaN NaN
00:00:26 32 NaN 5
00:00:30 25 14 10
df3 is what I would like to achieve. Here there is a timestamp for every index in df1 and df2. For each common index, where db2 is not NaN, we take the values, otherwise we preserve those stored in df1.
df1 >>> 00:00:15 15 6 NaN
df2 >>> 00:00:15 NaN NaN 2
df3 >>> 00:00:15 15 6 2
df1 >>> 00:00:00 20 15 5
df2 >>> 00:00:00 15 5 8
df3 >>> 00:00:00 15 5 8
For clarification see the above examples. I really can't find a way to do this -- for reference each dataframe has around 90 columns and 100k+ rows.
解决方案
先尝试结合:
df3 = df2.combine_first(df1)
print(df3)
A_Output B_Output C_Output
TimeStamp
00:00:00 15.0 5.0 8.0
00:00:04 16.0 NaN NaN
00:00:06 17.0 NaN 3.0
00:00:15 15.0 6.0 2.0
00:00:18 19.0 NaN NaN
00:00:20 20.0 NaN 5.0
00:00:21 14.0 NaN NaN
00:00:26 32.0 NaN 5.0
00:00:30 25.0 14.0 10.0
推荐阅读
- macos - MacOS init 上的 nvm 说:zsh compinit:不安全的目录,为列表运行 compaudit
- python - 如何从另一个列表中添加嵌套字典(NodeDataView)中的键、值?
- javascript - Vue-Router 不在单页应用程序中重新路由(Vue/Laravel)
- bash - 进入 bash 脚本中的第二个 if 语句时出错
- nestjs - 无法在创建用户 API 请求时使用 Nestjs 中的 Prisma 中间件对用户密码进行哈希处理
- sql-server - 是否可以将主数据服务 (MDS) 用作元数据存储库
- python - 如何在while循环中使用continue?
- macros - autoheader 创建与其他头文件中定义的其他变量冲突的 VERSION 宏
- selenium - 以下Java错误java.lang.ExceptionInInitializerError是否有解决方案?
- r - 如何使用 dplyr 使用行数据创建新列