Paras Ghai
Paras Ghai

Reputation: 373

How to compare columns in two dataframes in Python?

I have a problem where I want to check if first two columns of two dataframes are identical or not ? Let say I have dataframe1 with columns ["Date","Day","Volume"] and another dataframe2 with columns ["Date","Day"]. I want to check if these two data frames are having Date and Day in the same structure or not? How can I achieve this in the most optimized way?

Upvotes: 2

Views: 57

Answers (1)

jezrael
jezrael

Reputation: 862641

If want compare exactly same values, same index and same length between 2 columns use DataFrame.equals with subset of columns by list:

mask = df1[['Date','Day']].equals(df2[['Date','Day']])

If second df2 has only 2 columns:

mask = df1[['Date','Day']].equals(df2)

Upvotes: 3

Related Questions