5

how to print the result in the separate dataframe after comparing it with two columns in different dataframes.

consider two dataframes:

df1 = pd.DataFrame({'col1':['audi','cars']})  
df2 = pd.DataFrame({'col2':['audi','bike']})

print (df1)

    col1
0  audi
1  cars 

print (df2)

     col2
0   audi
1   bike

now the result should be in a different dataframe.

      col1  col2  result
0     audi  audi   no change
1     cars  bike   changed

1 Answer 1

16

Use concat with numpy.where:

df = pd.concat([df1, df2], axis=1)
df['result'] = np.where(df['col1'] == df['col2'], 'no change', 'changed')
print (df)
   col1  col2     result
0  audi  audi  no change
1  cars  bike    changed
2
  • when I used this I am getting the following warning SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead is there an another way
    – Jeril
    May 16, 2019 at 14:46
  • @Jeril - Obviously problem is in another code, check this
    – jezrael
    May 16, 2019 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.