Compare the name of the fields of a dataframe in python with pandas

1
import pandas as pd

main_file_path = '../input/train.csv'
df1 = pd.read_csv(main_file_path)
main_file_path2 = '../input/test.csv'
df2 = pd.read_csv(main_file_path2)

df1.columns
df2.columns

How can I know the fields they have in common and the fields that differ in both dataframe ?

    
asked by Michael Merchan 21.03.2018 в 21:28
source

1 answer

2

the most direct way I can think of.

Common fields:

set(df1.columns.values).intersection(set(df2.columns.values))

Fields in df1 and not in df2 :

set(df1.columns.values).difference(set(df2.columns.values))

and, conversely, fields in df2 but not in df1 :

set(df2.columns.values).difference(set(df1.columns.values))
    
answered by 21.03.2018 / 21:37
source