Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 479
Abdul Karim Khan Avatar asked Dec 23 '22 10:12

Abdul Karim Khan


2 Answers

I believe you need:

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

Or like @Zero pointed in comments:

df = df1[df1.columns & df2.columns]
like image 82
jezrael Avatar answered Dec 26 '22 00:12

jezrael


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
like image 37
Zero Avatar answered Dec 25 '22 23:12

Zero