[Python] How to compare dataframes and add to dataframe if neccessary

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    I have 2 dataframes that are the results of 2 queries, returned in JSON. One of the dataframes sap_scrapped looks like this and was generated by:

    sap_scrapped = pd.DataFrame(response.json()["d"]["results"]))


    Material Scrapped
    0 08-008099-00 2
    1 10-000001-00 5


    The other dataframe sap_warehouse looks like this and was generated by:

    sap_warehouse = pd.DataFrame(response.json()["d"]["results"]))


    Material SSP Scrapped
    0 10-000001-00 0 0
    1 10-789001-00 10 7


    I want to compare each value in the sap_scrapped 'Material' column to the sap_warehouse 'Material' column. If the scrapped material is not found anywhere in sap_warehouse, then I want to insert that missing material to the sap_warehouse dataframe. Using the data above, I'd want to insert 08-008099-00 into the sap_warehouse dataframe into the 'Material' column, zero in the SSP column and copy the 'Scrapped' value from the sap_scrapped dataframe (2) into 'Scrapped' in sap_warehouse.

    How can I do that?

    Continue reading...

Compartilhe esta Página