[Python] Extract values from a dataframe based on values in another dataframe

Discussão em 'Python' iniciado por Stack, Fevereiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Dataframe df like the following:

    Warehouse Date Count
    0 Delhivery Goa Warehouse 2022-05-12 83
    1 Delhivery Goa Warehouse 2022-05-15 1
    2 Delhivery Goa Warehouse 2022-05-18 100
    3 Delhivery Tauru Warehouse 2022-05-19 100
    4 Delhivery Tauru Warehouse 2022-05-20 100


    and another dataframe df_orig like the following:

    index Goa Tauru
    0 2022-05-12Delhivery Goa Warehouse 100.0 0.0
    1 2022-05-15Delhivery Goa Warehouse 100.0 0.0
    2 2022-05-18Delhivery Goa Warehouse 100.0 0.0
    3 2022-05-20Delhivery Tauru Warehouse 0.0 50.0
    4 2022-05-19Delhivery Tauru Warehouse 0.0 70.0


    How can I pick values from the df_orig columns based on combination of warehouse and Date columns of the df?

    Expected output:

    Warehouse Date Count original
    0 Delhivery Goa Warehouse 2022-05-12 83 100
    1 Delhivery Goa Warehouse 2022-05-15 1 100
    2 Delhivery Goa Warehouse 2022-05-18 100 100
    3 Delhivery Tauru Warehouse 2022-05-19 100 70
    4 Delhivery Tauru Warehouse 2022-05-20 100 50


    My approach:

    df['index1'] = str(df['Date']) + str(df['Warehouse'])
    original = []
    for index, row in df.iterrows():
    if row['index1'] == df_orig['index']:
    original.append(????)

    Continue reading...

Compartilhe esta Página