Ptar
Ptar

Reputation: 178

Concatenate and get all Column headers

I’d like to have a column name called SUBJECTS, before the one named STREAM.
Like this

SUBJECT  MEAN      RANK
ENG      59.2000      1
KIS      26.5474      3
MAT      56.6000      2

I’ve tried but this is the best I can get.

         SUBJECT    MEAN      
ENG      59.2000      1
KIS      26.5474      3
MAT      56.6000      2

I have this from a concatenation of

ENG       59.2000
KIS       26.5474
MAT       56.6000

And

ENG      1
KIS      3
MAT      2
dfs = pd.concat([dfM, dfR], axis=1, sort=False,ignore_index=False,keys=[‘SUBJECT’,'MEAN', 'RANK'])

The two items picked from the “keys” is The first two,SUBJECT, MEAN, RANK is not recorded.

Your answer gets me to where I want to be only that RANK is NaN now.

    SUBJ     MEAN RANK
0    ENG  59.2000  NaN
1    KIS  26.5474  NaN
2    MAT  56.6000  NaN

Upvotes: 0

Views: 136

Answers (2)

Ptar
Ptar

Reputation: 178

Thanks a lot for this. It worked for me though I had to change the common column to 'index', rather than 0. See below.

dfs = dfM.merge(dfR,how="left",on='index')

Upvotes: 1

vbhargav875
vbhargav875

Reputation: 887

You can do the following :

1) Merge the two dataframes instead of concat

dfs = dfM.merge(dfR,how="left",on=0)

2) Rename the columns :

dfs.columns = ['SUBJECT','MEAN','RANK']

Should give you this

 SUBJECT     MEAN  RANK
 ENG       59.2000     1
 KIS       26.5474     3
 MAT       56.6000     2 

Upvotes: 1

Related Questions