I have a dataframe called df
which has the following columns header of data:
date A B C D E F G H I
07/03/2016 2.08 1 NaN NaN 1029 2 2.65 4861688 -0.0388
08/03/2016 2.20 1 NaN NaN 1089 2 2.20 5770819 -0.0447
: :
09/03/2016 2.14 1 NaN NaN 1059 2 2.01 5547959 -0.0514
10/03/2016 2.25 1 NaN NaN 1089 2 1.95 4064482 -0.0520
Is there a way to change the order of the columns so that column F is moved to a position that is after column H. The resulting df
would look like:
date A B C D E F G H F I
07/03/2016 2.08 1 NaN NaN 1029 2 2.65 4861688 2 -0.0388
08/03/2016 2.20 1 NaN NaN 1089 2 2.20 5770819 2 -0.0447
: :
09/03/2016 2.14 1 NaN NaN 1059 2 2.01 5547959 2 -0.0514
10/03/2016 2.25 1 NaN NaN 1089 2 1.95 4064482 2 -0.0520
Use df.insert
with df.columns.get_loc
to dynamically determine the position of insertion.
col = df['F'] # df.pop('F') # if you want it removed
df.insert(df.columns.get_loc('H') + 1, col.name, col, allow_duplicates=True)
df
date A B C D E F G H F I
0 07/03/2016 2.08 1 NaN NaN 1029 2 2.65 4861688 2 -0.0388
1 08/03/2016 2.20 1 NaN NaN 1089 2 2.20 5770819 2 -0.0447
...
Use this :
df = df[['date','A','B','C','D','E','F','G','H','F','I']]
--- Edit
columnsName = list(df.columns)
F, H = columnsName.index('F'), columnsName.index('H')
columnsName[F], columnsName[H] = columnsName[H],columnsName[F]
df = df[columnsName]
Not for the author of this question, but perhaps for others.
col_list = df.columns.tolist() # list the columns in the df
col_list.insert(8, col_list.pop(col_list.index('F'))) # Assign new position (i.e. 8) for "F"
df = df.reindex(columns=col_list) # Now move 'F' to it's new position
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With