python - Shifting order of rows in Dataframe -


i trying make last 2 rows of dataframe df first 2 of dataframe previous first row becoming 3rd row after shift. because added rows [3,0.3232, 0, 0, 2,0.500], [6,0.3232, 0, 0, 2,0.500]. however, these added to end of df , hence become last 2 rows, when want them first two. wondering how this.

df = df.t df[0] = [3,0.3232, 0, 0, 2,0.500] df[1] = [6,0.3232, 0, 0, 2,0.500] df = df.t df = df.reset_index() 

you can call reindex , pass new desired order:

in [14]:  df = pd.dataframe({'a':['a','b','c']}) df out[14]:    0  1  b 2  c in [16]:  df.reindex([1,2,0]) out[16]:    1  b 2  c 0  

edit

another method use np.roll note returns np.array have explicitly select columns df overwrite them:

in [30]:     df = pd.dataframe({'a':['a','b','c'], 'b':np.arange(3)}) df  out[30]:     b 0   0 1  b  1 2  c  2  in [42]:     df[df.columns] = np.roll(df, shift=-1, axis=0) df  out[42]:     b 0  b  1 1  c  2 2   0 

the axis=0 param seems necessary otherwise column order not preserved:

in [44]:     df[df.columns] = np.roll(df, shift=-1) df  out[44]:     b 0  0  b 1  1  c 2  2  

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -