【问题标题】:Pandas: Merging two Dataframe, add columns and delete duplicate rowsPandas:合并两个数据框,添加列和删除重复行
【发布时间】:2020-12-21 05:39:56
【问题描述】:

我有两个数据框,比如 1 月和 2 月的材料库存报告:

一月报告

code  description    qty_jan   amount_jan

WP1   Wooden Part-1  1000      50000
MP1   Metal Part-1   500       5000
GL1   Glass-1        100       2500

二月报告

code  description    qty_feb   amount_feb

WP1   Wooden Part-1  1200      60000
MP2   Metal Part-2   300       3000
GL1   Glass-1        50        1250
GL2   Glass-2        200       5000

为了监控每个物料库存的进度,我想合并两个报告,如下:

code  description    qty_jan   amount_jan    qty_feb   amount_feb

WP1   Wooden Part-1  1000      50000         1200      60000
MP1   Metal Part-1   500       5000          0         0   
MP2   Metal Part-2   0         0             300       3000
GL1   Glass-1        100       2500          50        1250
GL2   Glass-2        0         0             200       5000 

注意:未在报告中列出的材料被视为零库存。

如何合并这两个报表?

【问题讨论】:

标签: pandas dataframe merge concatenation row


【解决方案1】:

您可以在DataFrame.merge 中使用外连接,然后将缺失值替换为0

df = df1.merge(df2, on=['code','description'], how='outer').fillna(0)
print (df)
v  code    description  qty_jan  amount_jan  qty_feb  amount_feb
0  WP1  Wooden Part-1   1000.0     50000.0   1200.0     60000.0
1  MP1   Metal Part-1    500.0      5000.0      0.0         0.0
2  GL1        Glass-1    100.0      2500.0     50.0      1250.0
3  MP2   Metal Part-2      0.0         0.0    300.0      3000.0
4  GL2        Glass-2      0.0         0.0    200.0      5000.0

concat 的另一个想法:

df = pd.concat([df1.set_index(['code','description']), 
                df2.set_index(['code','description'])], axis=1).fillna(0).reset_index()
print (df)
  code    description  qty_jan  amount_jan  qty_feb  amount_feb
0  GL1        Glass-1    100.0      2500.0     50.0      1250.0
1  GL2        Glass-2      0.0         0.0    200.0      5000.0
2  MP1   Metal Part-1    500.0      5000.0      0.0         0.0
3  MP2   Metal Part-2      0.0         0.0    300.0      3000.0
4  WP1  Wooden Part-1   1000.0     50000.0   1200.0     60000.0

【讨论】:

  • 非常快的回答。非常感谢。
猜你喜欢
  • 2011-11-18
  • 1970-01-01
  • 2019-01-21
  • 2017-01-06
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多