简体   繁体   中英

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', '23456']]

df_a = pd.DataFrame(a, columns=['id', 'sku', 'quantity '])
df_b = pd.DataFrame(b, columns =['id','sku'])

I need to compare the 'id and 'sku' columns from both dataframes and for those that match I need to update df_a['quantity'] to equal '0'.

So, something like an if statement?

if (df_a['id'] == df_b['id']) and (df_a['sku'] == df_b['sku']):
    df_a['quantity']=0

这应该做

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

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

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
0   10  12345   0
1   15  78910   0
2   8   23456   10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM