改善数据框的多个合并

时间:2018-11-20 10:14:32

标签: python pandas

我有以下数据框:

print(inventory_df)

dt_op        Prod_1  Prod_2 ... Prod_n
10/09/18       0        0         0
11/09/18       0        0         0
12/09/18       0        0         0

...

print(final_inspect)

    dt_op        Prod_1  
    10/09/18       10       
    11/09/18       2                
    12/09/18       5  

print(updated_df)
    dt_op        Prod_1  Prod_2 ... Prod_n
    10/09/18      10        0         0
    11/09/18       2        0         0
    12/09/18       5        0         0

    ...       

我正在尝试更新数据框“ inventory_df ”,并将其值包含在“ final_inspect ”中,以获取“ < strong> updated_df ”,其中:

final_inspect = pd.DataFrame(data = {'dt_op': inspect["dt_op"] , j: inventory}) 
final_inspect_1 = pd.DataFrame(data = {'dt_op':inventory_df.dt_op }) 

final_inspect_1 = final_inspect_1.merge(final_inspect, on = "dt_op", how = "left").fillna(0)
inventory_df = inventory_df.merge(final_inspect_1, on = ["dt_op", j], how = "outer").fillna(0)

inventory_df = inventory_df.drop_duplicates(subset=None, keep='first', inplace=False)

该解决方案很麻烦,但是熊猫的更新 功能似乎有效(库存_df.update(final_inspect))。 / p>

为了在多个(3000)项目上运行该解决方案,我该如何改进它?

updated_df size 必须与 inventory_df 相同,并且final_inspect的行数少于stock_df的行数

0 个答案:

没有答案