Home > other >  How to compare two dataframes' structures
How to compare two dataframes' structures

Time:04-28

I have two pandas dataframes and I want to compare their structures only. I tried to do this:

df0Info = df0.info()
df1Info = df1.info()
if df0Info == df1Info:
    print("They are same")
else:
    print("They are diff")

I found the result always is same whether the two dataframes structures are same or different. How can I get the correct result?

CodePudding user response:

pandas.DataFrame.info prints a summary of a DataFrame and returns None, so comparing outputs to one another will always be True because you're essentially testing None == None.

CodePudding user response:

Since df.info returns None, you can create the data required and compare those:

df0Info = pd.concat([df0.dtypes, df0.count()], axis=1)
df1Info = pd.concat([df1.dtypes, df1.count()], axis=1)
if df0Info.eq(df1Info).all().all():
    print("Same")
else:
    print("Different")
  • Related