Stacey
Stacey

Reputation: 5097

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

Upvotes: 9

Views: 16353

Answers (5)

k.ko3n
k.ko3n

Reputation: 954

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

Upvotes: 4

cs95
cs95

Reputation: 402323

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
...

Upvotes: 10

jpp
jpp

Reputation: 164623

This is one way via pd.DataFrame.iloc, which uses integer-location based indexing for selecting by position.

It's also a gentle reminder that pandas integer indexing is based on numpy.

import pandas as pd
import numpy as np

df = pd.DataFrame(columns=list('ABCDEFGHI'))

cols = np.insert(np.arange(df.shape[1]),
                 df.columns.get_loc('H')+1,
                 df.columns.get_loc('F'))

res = df.iloc[:, cols]

print(res)

Empty DataFrame
Columns: [A, B, C, D, E, F, G, H, F, I]
Index: []

Upvotes: 1

Vla Mai
Vla Mai

Reputation: 332

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]

Upvotes: 5

llllllllll
llllllllll

Reputation: 16404

You can use:

df.reindex(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'F', 'I'], axis=1)

Upvotes: 0

Related Questions