MatMorPau22
MatMorPau22

Reputation: 350

Pandas: Force position of column in DataFrame (without knowing all columns)

Let's say I have a DataFrame and don't know the names of all columns. However, I know there's a column called "N_DOC" and I want this to be the first column of the DataFrame - (while keeping all other columns, regardless its order).

How can I do this?

Upvotes: 2

Views: 171

Answers (3)

s3dev
s3dev

Reputation: 9701

Here's a simple, one line, solution using DataFrame masking:

import pandas as pd

# Building sample dataset.
cols = ['N_DOCa', 'N_DOCb', 'N_DOCc', 'N_DOCd', 'N_DOCe', 'N_DOC']
df = pd.DataFrame(columns=cols)

# Re-order columns.
df = df[['N_DOC'] + df.columns.drop('N_DOC').tolist()]

Before:

Index(['N_DOCa', 'N_DOCb', 'N_DOCc', 'N_DOCd', 'N_DOCe', 'N_DOC'], dtype='object')

After:

Index(['N_DOC', 'N_DOCa', 'N_DOCb', 'N_DOCc', 'N_DOCd', 'N_DOCe'], dtype='object')

Upvotes: 1

jezrael
jezrael

Reputation: 862901

Use DataFrame.insert with DataFrame.pop for extract column:

df = pd.DataFrame({
        'A':list('abcdef'),
         'B':[4,5,4,5,5,4],
         'C':[7,8,9,4,2,3],
         'N_DOC':[1,3,5,7,1,0],
         'E':[5,3,6,9,2,4],
         'F':list('aaabbb')
})

c = 'N_DOC'
df.insert(0, c, df.pop(c))

Or:

df.insert(0, 'N_DOC', df.pop('N_DOC'))

print (df)
   N_DOC  A  B  C  E  F
0      1  a  4  7  5  a
1      3  b  5  8  3  a
2      5  c  4  9  6  a
3      7  d  5  4  9  b
4      1  e  5  2  2  b
5      0  f  4  3  4  b

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148975

You can reorder the columns of a datframe with reindex:

cols = df.columns.tolist()
cols.remove('N_DOC')
df.reindex(['N_DOC'] + cols, axis=1)

Upvotes: 3

Related Questions