【问题标题】:pandas combine_first but always overwrite熊猫 combine_first 但总是覆盖
【发布时间】:2016-02-02 03:23:13
【问题描述】:

可以改进以下内容吗?

它实现了将值从df2 复制到可以匹配索引的df1 的预期结果。它似乎效率低下且笨重。

df1 = pd.DataFrame([[0, 1, 2], [3, 4, 5]], index=pd.MultiIndex.from_tuples(['AB', 'AC']), columns=['X', 'Y', 'Z'])
df2 = pd.DataFrame([102, 103], index=pd.MultiIndex.from_tuples(['AC', 'AD']), columns=['Y'])
desired = df2.combine_first(df1).combine_first(df2)

print(df1)
print(df2)
print(desired)

Output:

df1
     X  Y  Z
A B  0  1  2
  C  3  4  5

df2
       Y
A C  102
  D  103

desired
      X    Y   Z
A B   0    1   2
  C   3  102   5
  D NaN  103 NaN

我最接近使用切片的是

print(df1.loc[df2.index, df2.columns])  # This works, demonstrated lhs of below is OK
df1.loc[df2.index, df2.columns] = df2   # This fails, as does df2.values

【问题讨论】:

  • 为什么需要第二个combine_first?不是desired.equals(df2.combine_first(df1) )== True吗?
  • 为什么会这样 - 我将重新审视我的“真实”问题,并通过一个小例子来正确说明我遇到的问题。 (或者也许我会发现我把事情复杂化了!)

标签: python pandas merge


【解决方案1】:

为什么不使用merge ??

>>df3 = pd.merge(df1, df2, left_index=True, right_index=True, how='outer')
>>df3
       X  Y_x    Z    Y_y
A B  0.0  1.0  2.0    NaN
  C  3.0  4.0  5.0  102.0
  D  NaN  NaN  NaN  103.0
>>df3['Y'] = df3['Y_y'].combine_first(df3['Y_x'])
>>df3.drop(['Y_x', 'Y_y'], axis=1)
       X    Z      Y
A B  0.0  2.0    1.0
  C  3.0  5.0  102.0
  D  NaN  NaN  103.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多