Tyrone_Slothrop
Tyrone_Slothrop

Reputation: 177

Comparison between two dataframes?

I have an auditing script that takes a sample from an excel file. The code work just fine, but I'm trying to make a comparison between two dataframes, the initial one and the sampled one, and mark items as "Audit" in the original frame. I've used both codes below to no success:

df['Retailer Item ID'] in final['Retailer Item ID'], df['Track Item'] = 
'Audit'

if df.loc[df['Retailer Item ID'] == final['Retailer Item ID']:
    df['Track Item'] = 'Audit']

Any idea what I'm doing wrong?

Upvotes: 0

Views: 65

Answers (3)

Nev1111
Nev1111

Reputation: 1049

You can do a merge and pass the indicator as a parameter:

df_merged=pd.merge(df,final,left_on='Track Item', right_on='Retailer Item ID', how='left',indicator=True)

You will see a "True" value in the indicator column for each item that you want to include in your audit sample.

Upvotes: 0

Tyrone_Slothrop
Tyrone_Slothrop

Reputation: 177

Solution: df.loc[df['Retailer Item ID'].isin(final['Retailer Item ID']), 'Track Item'] = 'Audit'

Upvotes: 0

pc_pyr
pc_pyr

Reputation: 626

You could try np.where

df['Track Item']=np.where(df['Retailer Item ID'] == final['Retailer Item ID'], 'Audit','null')

Upvotes: 2

Related Questions