【问题标题】:copying data from one pandas DF to another将数据从一个 pandas DF 复制到另一个
【发布时间】: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()
  • 嗨......我按照你建议的解决方案,但我遇到了错误,这是我没想到的。我更新了问题来解释。感谢您在寻找工作方面的帮助。

标签: python pandas


【解决方案1】:

我找到了解决问题的方法,灵感来自我在这篇文章中找到的回复:Python Pandas update a dataframe value from another dataframe

我应用了几个 for 循环来处理多列合并和删除情况并满足我的需要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 1970-01-01
    相关资源
    最近更新 更多