I have the following DataFrame
(df
):
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.rand(10, 5))
I add more column(s) by assignment:
df['mean'] = df.mean(1)
How can I move the column mean
to the front, i.e. set it as first column leaving the order of the other columns untouched?
Simply do,
This function avoids you having to list out every variable in your dataset just to order a few of them.
It takes two arguments, the first is the dataset, the second are the columns in the data set that you want to bring to the front.
So in my case I have a data set called Frame with variables A1, A2, B1, B2, Total and Date. If I want to bring Total to the front then all I have to do is:
If I want to bring Total and Date to the front then I do:
EDIT:
Another useful way to use this is, if you have an unfamiliar table and you're looking with variables with a particular term in them, like VAR1, VAR2,... you may execute something like:
The simplest way would be to change the order of the column names like this
df = df[['mean', Col1,Col2,Col3]]
I ran into a similar question myself, and just wanted to add what I settled on. I liked the
reindex_axis() method
for changing column order. This worked:An alternate method based on the comment from @Jorge:
Although
reindex_axis
seems to be slightly faster in micro benchmarks thanreindex
, I think I prefer the latter for its directness.Here is a function to do this for any number of columns.
In your case,
will do exactly what you want.
In my case (general form):
update Jan 2018
If you want to use
reindex
: