-1

I have 2 similar dataframes that I would like to compare each row of the 1st dataframe with the 2nd based on condition. The dataframe looks like this:

Dataframe

Based on this comparison I would like to generate a similar dataframe with a new column 'change' containing the changes based on the following conditions:

if the rows have similar values then 'change'='identical' otherwise if the date changed then 'change'='new date'.

JanSeiler
  • 5
  • 2
  • 2
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on how to ask a good question may also be useful. – yatu May 08 '19 at 12:38
  • 2
    It would help if you could provide a more complete example, and include what you have tried so far. In this case: two example input dataframes and the desired output dataframe. – holmrenser May 08 '19 at 12:39

1 Answers1

0

Here is an easy workaround.

# Import pandas library 
import pandas as pd 

# One dataframe
data = [['foo', 10], ['bar', 15], ['foobar', 14]] 
df = pd.DataFrame(data, columns = ['Name', 'Age']) 

# Another similar dataframe but foo age is 13 this time 
data = [['foo', 13], ['bar', 15], ['foobar', 14]] 
df2 = pd.DataFrame(data, columns = ['Name', 'Age'])
df3 = df2.copy()
for index, row in df.iterrows():
     if df.at[index,'Age'] != df2.at[index,'Age']:
          df3.at[index,'Change']="Changed"
df3["Change"].fillna("Not Changed",inplace = True)
print(df3)

Here is the output

     Name  Age       Change
0     foo   13      Changed
1     bar   15  Not Changed
2  foobar   14  Not Changed
Ismail
  • 331
  • 3
  • 10
  • Heya, welcome to Stack Overflow! Could you please take a moment to [edit] your answer and explain how it works? Thank you. – grooveplex May 08 '19 at 13:32