Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a dataframe column and changing column order

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
like image 916
Stacey Avatar asked Jun 02 '18 20:06

Stacey


3 Answers

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
...
like image 77
cs95 Avatar answered Sep 18 '22 22:09

cs95


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]
like image 28
Vla Mai Avatar answered Sep 19 '22 22:09

Vla Mai


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
like image 24
k.ko3n Avatar answered Sep 19 '22 22:09

k.ko3n