Stupid420
Stupid420

Reputation: 1419

Matching the column names of two pandas data-frames in python

I have two pandas dataframes with names df1 and df2 such that `

df1: a      b    c    d
     1      2    3    4
     5      6    7    8

and

df2:      b    c   
          12   13   

I want the result be like

result:    b    c    
           2    3    
           6    7    

Here it should be noted that a b c d are the column names in pandas dataframe. The shape and values of both pandas dataframe are different. I want to match the column names of df2 with that of column names of df1 and select all the rows of df1 the headers of which are matched with the column names of df2.. df2 is only used to select the specific columns of df1 maintaining all the rows. I tried some code given below but that gives me an empty index.

df1.columns.intersection(df2.columns)

The above code is not giving me my resut as it gives index headers with no values. I want to write a code in which I can give my two dataframes as input and it compares the columns headers for selection. I don't have to hard code column names.

Upvotes: 10

Views: 10937

Answers (3)

Zero
Zero

Reputation: 76917

Or, use reindex

In [594]: df1.reindex(columns=df2.columns)
Out[594]:
   b  c
0  2  3
1  6  7

Also as

In [595]: df1.reindex(df2.columns, axis=1)
Out[595]:
   b  c
0  2  3
1  6  7

Upvotes: 2

jezrael
jezrael

Reputation: 862511

I believe you need:

df = df1[df1.columns.intersection(df2.columns)]

Or like @Zero pointed in comments:

df = df1[df1.columns & df2.columns]

Upvotes: 10

A.Kot
A.Kot

Reputation: 7903

Alternatively to intersection:

df = df1[df1.columns.isin(df2.columns)]

Upvotes: 1

Related Questions