Skip to content Skip to sidebar Skip to footer

Compare Two Pandas Dataframes And Update One, Depending On Results

I have the following (simplified) data; import pandas as pd a = [['10', '12345', '4'], ['15', '78910', '3'], ['8', '23456', '10']] b = [['10', '12345'], ['15', '78910'], ['9', '23

Solution 1:

This should do it

df_a.loc[(df_b['id'] == df_a['id']) & (df_a['sku'] == df_b['sku']), 'quantity '] = 0

Solution 2:

Another approach using pandas merge:

df_a.loc[pd.merge(df_a, df_b, on = ['id', 'sku'] , how='left',
    indicator=True)['_merge'] == 'both', 'quantity'] = 0

df_a
    id  sku quantity
010123450115789100282345610

Solution 3:

Not the most elegant way, but will do the trick if dataframes have different shapes.

a_id_sku = df_a.id + df_a.sku
b_id_sku = df_b.id + df_b.sku

df_a.loc[a_id_sku.isin(b_id_sku), 'quantity '] = 0

Let me know if this worked

Post a Comment for "Compare Two Pandas Dataframes And Update One, Depending On Results"