'How to compare two dataframes' structures
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?
Solution 1:[1]
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.
Solution 2:[2]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BeRT2me |
| Solution 2 | not_speshal |
