Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving desired row to the top of pandas Data Frame

Tags:

python

pandas

In pandas, how can I copy or move a row to the top of the Data Frame without creating a copy of the Data Frame?

For example, I managed to do almost what I want with the code below, but I have the impression that there might be a better way to accomplish this:

import pandas as pd

df = pd.DataFrame({'Probe':['Test1','Test2','Test3'], 'Sequence':['AATGCGT','TGCGTAA','ATGCATG']})

df

   Probe Sequence
0  Test1  AATGCGT
1  Test2  TGCGTAA
2  Test3  ATGCATG

df_shifted = df.shift(1)

df_shifted

   Probe Sequence
0    NaN      NaN
1  Test1  AATGCGT
2  Test2  TGCGTAA


df_shifted.ix[0] = df.ix[2]

df_shifted

   Probe Sequence
0  Test3  ATGCATG
1  Test1  AATGCGT
2  Test2  TGCGTAA
like image 460
ropolo Avatar asked Dec 15 '22 04:12

ropolo


1 Answers

Try this. You don't need to make a copy of the dataframe.

df["new"] = range(1,len(df)+1)

   Probe Sequence  new
0  Test1  AATGCGT    1
1  Test2  TGCGTAA    2
2  Test3  ATGCATG    3


df.ix[2,'new'] = 0
df.sort_values("new").drop('new', axis=1)

   Probe Sequence
2  Test3  ATGCATG
0  Test1  AATGCGT
1  Test2  TGCGTAA

Basically, since you can't insert the row into the index at 0, create a column so you can.

If you want the index ordered, use this:

df.sort_values("new").reset_index(drop='True').drop('new', axis=1)

   Probe Sequence
0  Test3  ATGCATG
1  Test1  AATGCGT
2  Test2  TGCGTAA

Edit: df.ix is deprecated. Here's the same method with .loc.

df["new"] = range(1,len(df)+1)
df.loc[df.index==2, 'new'] = 0
df.sort_values("new").drop('new', axis=1)
like image 79
Merlin Avatar answered Dec 26 '22 16:12

Merlin