When I add a column df[xx] it is added at the end of the dataframe
What I would like to do is add the column "xx" right after column "x" ("x" could be anywhere in the dataframe) Is there an easy way to do this?
When I add a column df[xx] it is added at the end of the dataframe
What I would like to do is add the column "xx" right after column "x" ("x" could be anywhere in the dataframe) Is there an easy way to do this?
df = pd.DataFrame(columns=['x', 'y', 'xx'])
df
x   y   xx
df = df.reindex_axis(sorted(df.columns), axis=1)
df
x   xx  y
EDIT:
df = pd.DataFrame(columns=['x', 'y', 'xx', 'q', 'q1'])
row = pd.Series({'x':1,'y':2,'xx':3,'q':4,'q1':5})
df = df.append(row, ignore_index=True)
df
   x    y   xx  q   q1
0   1   2   3   4   5
df = df.reindex_axis(['q', 'y', 'x', 'xx', 'q1'], axis=1)
   q    y   x   xx  q1
0   4   2   1   3   5
