I have two dataframes that look like this:
df_1 = pd.DataFrame({
\'A\' : [1.0, 2.0, 3.0, 4.0],
\'B\' : [100, 200, 300, 400],
\'C\' : [2, 3, 4, 5]
Pandas index object have set-like properties, so you can directly do:
df_2.columns.difference(df_1.columns)
Index([u'D'], dtype='object')
You can also use operators like &|^
to compute intersection, union and symmetric difference:
df_1.columns & df_2.columns
Index([u'B', u'C'], dtype='object')
df_1.columns | df_2.columns
Index([u'A', u'B', u'C', u'D'], dtype='object')
df_1.columns ^ df_2.columns
Index([u'A', u'D'], dtype='object')
There use to be the -
operator for difference, now deprecated:
df_2.columns - df_1.columns
FutureWarning: using '-' to provide set differences with Indexes is deprecated, use .difference()
Index([u'D'], dtype='object')