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)
Author by
Aptha Gowda
Updated on June 06, 2022Comments
-
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 about 6 yearsUse the align method
-
Yunnosch almost 3 yearsHi 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 almost 3 yearsWhile 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.