【问题标题】:Updating a pandas dataframe with a new dataframe使用新数据框更新 pandas 数据框
【发布时间】:2018-09-18 10:05:16
【问题描述】:

Ye with Pandas Perspicacity,

我正在尝试用另一个数据框更新一个简单的数据框,但遇到了麻烦。我有一个要更新的主数据框:

Master_df:

          color     tastey
name                      
Apples      Red     Always
Avocados  Black  Sometimes
Anise     Brown        NaN

我有一些新数据想用来更新这个数据框。它可能会追加新列、添加新行或更新旧值:

New_df:

          color   tastey   price
name                            
Bananas  Yellow      NaN  Medium
Apples      Red  Usually     Low
Berries     Red      NaN    High

我想合并这两个数据框,使更新后的数据框看起来像:

Desired_df:

           color     tastey   price
name                               
Apples       Red     Always     Low
Avocados   Black  Sometimes     NaN
Anise      Brown        NaN     NaN
Bananas   Yellow        NaN  Medium
Berries      Red        NaN    High

我玩过 many 不同的命令,但我仍然在努力:

  • 不会丢失我加入的索引值。
  • 让公共列组成一个有味的列,而不是有味_x 和有味_y。
  • 拥有来自新行的新数据。
  • 不必对新列或新行的名称进行硬编码。

最后,(虽然此示例中未显示)我需要加入多个列。即我需要使用 3 列来形成我的唯一键。 (虽然我确信上述示例的解决方案会扩展到这种情况。)

我真诚地感谢任何帮助或指点!我希望上面的例子很清楚。

干杯,

熊猫针头。

edit1:我相信这个问题与以前提出的问题不同,因为当我使用combine_first 时,我得到了这个:

>>> Master_df.combine_first(New_df)

          color     tastey
name                      
Apples      Red     Always
Avocados  Black  Sometimes
Anise     Brown        NaN

Edit2:好的,我越来越近了,但还没有!我不想生成 _x_y 列。我希望它们成为一列,在发生冲突时从 New_df 获取数据。

>>> updated = pd.merge(Master_df, New_df, how="outer", on=["name"])
       name color_x   tastey_x color_y tastey_y   price
0    Apples     Red     Always     Red  Usually     Low
1  Avocados   Black  Sometimes     NaN      NaN     NaN
2     Anise   Brown        NaN     NaN      NaN     NaN
3   Bananas     NaN        NaN  Yellow      NaN  Medium
4   Berries     NaN        NaN     Red      NaN    High

Edit3:Here's an image of what I'm trying to do. 重要的是,除了键之外,我不必对列名(“A”、“B”等)进行硬编码。

附:代码如下。

import pandas as pd
import numpy as np

Master_data = {
    'name' : ['Apples', 'Avocados', 'Anise'],
    'color' : ['Red', 'Black', 'Brown'],
    'tastey' : ['Always', 'Sometimes', np.NaN]
}

Master_df = pd.DataFrame(Master_data, columns = ['name', 'color', 'tastey'])
Master_df = Master_df.set_index('name')

print(Master_df)

newData = {
    'name' : ['Bananas', 'Apples', 'Berries'],
    'color' : ['Yellow', 'Red', 'Red'],
    'tastey' : [np.NaN, 'Usually', np.NaN],
    'price' : ['Medium', 'Low', 'High']
}

New_df = pd.DataFrame(newData, columns = ['name', 'color', 'tastey', 'price'])
New_df = New_df.set_index('name')

print(New_df)

Desired_data = {
    'name' : ['Apples', 'Avocados', 'Anise', 'Bananas', 'Berries'],
    'color' : ['Red', 'Black', 'Brown', 'Yellow', 'Red'],
    'tastey' : ['Always', 'Sometimes', np.NaN, np.NaN, np.NaN],
    'price' : ['Low', np.NaN, np.NaN, 'Medium', 'High']
}

Desired_df = pd.DataFrame(Desired_data, columns = ['name', 'color', 'tastey', 'price'])
Desired_df = Desired_df.set_index('name')

print(Desired_df)

【问题讨论】:

    标签: python pandas dataframe merge


    【解决方案1】:

    您可以使用pd.DataFrame.update(就地操作)之前 pd.DataFrame.combine_first:

    New_df.update(Master_df)
    
    res = New_df.combine_first(Master_df)
    
    #            color   price     tastey
    # name                               
    # Anise      Brown     NaN        NaN
    # Apples       Red     Low     Always
    # Avocados   Black     NaN  Sometimes
    # Bananas   Yellow  Medium        NaN
    # Berries      Red    High        NaN
    

    【讨论】:

    • 谢谢!这里的问题是 Master_df 和 New_df 的顺序。这几天我一直在摸不着头脑。
    猜你喜欢
    • 2016-04-02
    • 2019-07-26
    • 2017-06-27
    • 2019-07-09
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多