2

I have foll 2 dataframes:

df_A A B C D 12 23 34 45 22 32 54 87 

and,

df_B B C 23 34 32 54 

How do I select the columns in df_A which are not present in df_B, resulting in following dataframe (you can assume that df_B is a subset of df_A):

A D 12 45 22 87 

I tried this:

df_A[df_A.columns.values <> df_B.columns.values] 

but it results in a KeyError

1 Answer 1

3
>>> df_A.columns.difference(df_B.columns) Index(['A', 'D'], dtype='object') 

will give you the columns, and you can do:

>>> cols = df_A.columns.difference(df_B.columns) >>> df_A[cols] A D 0 12 45 1 22 87 
Sign up to request clarification or add additional context in comments.

1 Comment

I just tried this and I still get all the columns to retrieve as opposed to just the difference, any idea why that could be?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.