Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas : compare two columns for equality and result in third dataframe

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
like image 797
ravi Avatar asked Dec 04 '22 19:12

ravi


1 Answers

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
like image 196
jezrael Avatar answered May 01 '23 13:05

jezrael