Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare column names of Pandas Dataframe

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??

like image 850
Aptha Gowda Avatar asked May 06 '18 19:05

Aptha Gowda


1 Answers

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)
like image 125
jpp Avatar answered Oct 15 '22 03:10

jpp