Compare column names of Pandas Dataframe

11,761

pandas.Index objects, including dataframe columns, have useful set-like methods, such as intersection and difference.

For example, given dataframes train and test:

train_cols = train.columns
test_cols = test.columns

common_cols = train_cols.intersection(test_cols)
train_not_test = train_cols.difference(test_cols)
Share:
11,761
Aptha Gowda
Author by

Aptha Gowda

Updated on June 06, 2022

Comments

  • Aptha Gowda
    Aptha Gowda almost 2 years

    How to compare column names of 2 different Pandas data frame. I want to compare train and test data frames where there are some columns missing in test Data frames??

  • piRSquared
    piRSquared about 6 years
    Use the align method
  • Yunnosch
    Yunnosch almost 3 years
    Hi Nath. Please make more obvious the functional difference to the solution proposed in the highly upvoted answer by jpp. Explain what benedit your solution offers in comparisopn. Currently this gives an impression of being functionally identical to an upvoted answer with no explanation. Because of this impression it risks being downvoted.
  • blazej
    blazej almost 3 years
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.