【发布时间】:2019-05-01 00:12:17
【问题描述】:
我有 2 个数据框 .... big_df 和 small_df
big_df
------
Typ col1 col2 col3 ...
A None None None ...
B None None None ...
A None None None ...
C None None None ...
B None None None ...
D None None None ...
E None None None ...
F None None None ...
.
.
.
small_df
------
Typ col1 col3 col8 ...
A 1.2 'a' 3
E 2.2 'z' 5
L 0.5 'w' 4
.
.
.
我需要使用 small_df 中的值有效地更新 big_df 字段。
Typ 在 big_df 中不是唯一的。
两个 DF(s) 当前都以数字索引 ...0,1,2,3 .... 等等
尝试通过 Typ 重新索引两个 DF(s) 将抛出:
ValueError: 无法从重复的轴重新索引
如果有任何关于最佳方式的建议/代码示例,我将不胜感激。
【问题讨论】:
-
big_df.update(small_df)然后print(big_df)?? -
这很棒......'key'是一列(不是索引)。如何确保在更新之前匹配密钥?谢谢
-
像
big_df=big_df.set_index('key')一样使用set_index(),其他df也一样,然后是update() -
嗨......我按照你建议的解决方案,但我遇到了错误,这是我没想到的。我更新了问题来解释。感谢您在寻找工作方面的帮助。