python - Move given row to end of DataFrame -


i take given row dataframe , prepend or append same dataframe.

my code below that, i'm not sure if i'm doing right way or if there easier, better, faster way?

testdf = df.copy() #get row  target_row = testdf.ix[[2],:] #del row df testdf.drop([testdf.index[2]], axis=0, inplace=true) #concat original row end or start of df newdf = pd.concat([testdf, target_row], axis=0) 

thanks

rather concat assign directly df after shifting, use iloc reference position want assign row, have call squeeze assign values , lose original index value otherwise it'll raise valueerror:

in [210]: df = pd.dataframe({'a':np.arange(5)}) df  out[210]:    0  0 1  1 2  2 3  3 4  4  in [206]: target_row = df.ix[[2],:] target_row  out[206]:    2  2  in [211]: df = df.shift() df.iloc[0] = target_row.squeeze() df  out[211]:    0  2 1  0 2  1 3  2 4  3 

edit

to insert @ end:

in [255]: df = pd.dataframe({'a':np.arange(5)}) target_row = df.ix[[2],:] df = df.shift(-1) df.iloc[-1] = target_row.squeeze() df  out[255]:    0  1 1  2 2  3 3  4 4  2 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -