如何在添加另一个的同时基于列合并DataFrames

时间:2019-10-31 16:32:28

标签: python pandas

我有以下模拟DataFrame:

df1:

ID    FILLER1    FILLER2    QUANTITY

01      123        132         12
02      123        132         5
03      123        132         10

df2:

ID    FILLER1    FILLER2    QUANTITY

01      123        132         +1
02      123        132         -1

这将导致DF1的“数量”将得出13、4和10。

请提前寻求任何帮助!

1 个答案:

答案 0 :(得分:1)

问题不是很清楚,但是如果我明白您要在这里做的事情,那就是一种方法:

# A left join and filling 0 instead of NaN for that third row
In [19]: merged = df1.merge(df2, on=['ID', 'FILLER1', 'FILLER2'], how='left').fillna(0)

In [20]: merged
Out[20]:
   ID  FILLER1  FILLER2  QUANTITY_x  QUANTITY_y
0   1      123      132          12         1.0
1   2      123      132           5        -1.0
2   3      123      132          10         0.0

# Adding new quantity column
In [21]: merged['QUANTITY'] = merged['QUANTITY_x'] + merged['QUANTITY_y']

In [22]: merged
Out[22]:
   ID  FILLER1  FILLER2  QUANTITY_x  QUANTITY_y  QUANTITY
0   1      123      132          12         1.0      13.0
1   2      123      132           5        -1.0       4.0
2   3      123      132          10         0.0      10.0

# Removing _x and _y columns
In [23]: merged = merged[['ID', 'FILLER1', 'FILLER2', 'QUANTITY']]

In [24]: merged
Out[24]:
   ID  FILLER1  FILLER2  QUANTITY
0   1      123      132      13.0
1   2      123      132       4.0
2   3      123      132      10.0