user3677949
user3677949

Reputation: 13

Change the order of columns in a dataframe

I have two data-frames

df1

Index([u'CASE_NO', u'DECISION_DATE', u'CASE_STATUS', u'APPLICATION_TYPE', u'EMPLOYER_NAME', u'EMPLOYER_ADDRESS_1', u'EMPLOYER_ADDRESS_2', u'EMPLOYER_CITY', u'EMPLOYER_STATE', u'EMPLOYER_POSTAL_CODE', u'2007_NAICS_US_CODE', u'2007_NAICS_US_TITLE', u'US_ECONOMIC_SECTOR', u'PW_SOC_CODE', u'PW_SOC_TITLE', u'PW_JOB_TITLE_9089', u'PW_LEVEL_9089', u'PW_SOURCE_NAME_9089', u'PW_AMOUNT_9089', u'PW_UNIT_OF_PAY_9089', u'WAGE_OFFER_FROM_9089', u'WAGE_OFFER_TO_9089', u'WAGE_OFFER_UNIT_OF_PAY_9089', u'JOB_INFO_WORK_CITY', u'JOB_INFO_WORK_STATE', u'COUNTRY_OF_CITZENSHIP', u'CLASS_OF_ADMISSION'], dtype='object')

df2

Index([u'CASE_NO', u'DECISION_DATE', u'CASE_STATUS', u'APPLICATION_TYPE', u'EMPLOYER_NAME', u'EMPLOYER_ADDRESS_1', u'EMPLOYER_CITY', u'EMPLOYER_STATE', u'EMPLOYER_POSTAL_CODE', u'2007_NAICS_US_CODE', u'2007_NAICS_US_TITLE', u'US_ECONOMIC_SECTOR', u'PW_SOC_CODE', u'PW_SOC_TITLE', u'PW_JOB_TITLE_9089', u'PW_LEVEL_9089', u'PW_SOURCE_NAME_9089', u'PW_AMOUNT_9089', u'PW_UNIT_OF_PAY_9089', u'WAGE_OFFER_FROM_9089', u'WAGE_OFFER_TO_9089', u'WAGE_OFFER_UNIT_OF_PAY_9089', u'JOB_INFO_WORK_CITY', u'JOB_INFO_WORK_STATE', u'COUNTRY_OF_CITZENSHIP', u'CLASS_OF_ADMISSION'], dtype='object')

I am trying to concat the two dataframes but the columns are not in the same order in both the dataframes. I came across this question How to change the order of DataFrame columns?

and I think I am thinking if there is something else to do more efficiently.

df2 columns should be reordered to match the df1 columns.

Upvotes: 0

Views: 185

Answers (1)

BrenBarn
BrenBarn

Reputation: 251568

If the column names are the same but just in a different order, you can do df2[df1.columns].

Upvotes: 2

Related Questions