0
I have a question, and I’d like your help. I have two Dataframe and I need to compare if some columns of these Dataframe are the same and, if they are the same, then I need to store this record in another dataframe. That is, I need to create a new Dataframe from the comparison of another two. The example refers to df1 and df2 (need to compare 4 criteria - gender, age, race and schooling) and after the comparison create df3 with the records that were true in the comparison. In the case of the example below, df3 would be formed by the index record 0 of df1 and by the record of the index 0 df2, since they are equal in the criteria mentioned.
import pandas as pd
df1 = pd.DataFrame({"gender": ['f','m','f'],
"age": [11,22,39],
"raca": ['C','nC','nC'],
"escolaridade": ['F','S','M'],
"var1":["yes", "yes", "no"],
"var2":["no", "yes", "yes"],
"var3":["no", "no", "no"],
"classe":["no", "yes", "no"]})
df2 = pd.DataFrame({"gender": ['f','f','m'],
"age": [11,22,40],
"raca": ['C','C','nC'],
"escolaridade": ['F','M','M'],
"var1":["yes", "yes", "no"],
"var2":["no", "no", "yes"],
"var3":["no", "no", "yes"],
"classe":["yes", "yes", "no"]})
Has a sample of Dataframes and a [mcve] of your attempt even not working? Probably your problem should be solved with
DataFrame.merge()
but without seeing the logic of what you’re doing it’s hard to say.– Augusto Vasques
Lili, since we don’t have the real case here, take a look at this link
– Paulo Marques