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

MatMorPau22 :

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?

Serge Ballesta :

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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169660&siteId=1