使用来自不同数据框的值更新特定 Pandas 行

Update Specific Pandas Rows with Value from Different Dataframe

我有一个包含预算数据的 pandas 数据框,但我的销售数据位于另一个大小不同的数据框中。如何在我的预算数据中更新我的销售数据?我如何编写条件以便它进行这些更新?

DF budget:
        cust   type   loc   rev   sales   spend
    0   abc    new    north 500   0       250
    1   def    new    south 700   0       150
    2   hij    old    south 700   0       150

DF sales:
        cust    type   loc    sales
    0   abc     new    north  15
    1   hij     old    south  18

DF budget outcome:
        cust   type   loc   rev   sales   spend
    0   abc    new    north 500   15      250
    1   def    new    south 700   0       150
    2   hij    old    south 700   18      150

有什么想法吗?

假设 'cust' 列在您的其他 df 中是唯一的,您可以在将索引设置为 'cust' 列后在销售 df 上调用 map,这将映射预算 df 中的每个 'cust' 到它的销售价值,另外你会得到 NaN 那里有缺失值所以你调用 fillna(0) 来填充这些值:

In [76]:

df['sales'] = df['cust'].map(df1.set_index('cust')['sales']).fillna(0)
df
Out[76]:
  cust type    loc  rev  sales  spend
0  abc  new  north  500     15    250
1  def  new  south  700      0    150
2  hij  old  south  700     18    150