用新的datafram更新pandas数据帧

2024-04-25 05:58:03 发布

您现在位置:Python中文网/ 问答频道 /正文

叶与熊猫的敏锐

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

主数据框:

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

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

新数据:

^{pr2}$

我想合并这两个数据帧,以便更新后的数据帧看起来像:

           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不同的命令,但我仍在努力:

  • 不会丢失我加入的索引值。在
  • 让公共栏组成一个tastey列,而不是tastey_x和tastey_y
  • 从新行获取新数据。在
  • 不必硬编码新列或新行的名称。在

最后,(虽然本例中没有显示)我需要在多个列上联接。i、 我需要使用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'等),这一点很重要。在

p.S.代码如下。在

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)

Tags: 数据namemasterdfnewnprednan
1条回答
网友
1楼 · 发布于 2024-04-25 05:58:03

您可以在^{}之前使用^{}(就地操作)

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

相关问题 更多 >