I would like to take a given row from a DataFrame and prepend or append to the same DataFrame.
My code below does just that, but I'm not sure if I'm doing it the right way or if there is an easier, better, faster way?
testdf = df.copy()
#get row
target_row = testdf.ix[[2],:]
#del row from df
testdf.drop([testdf.index[2]], axis=0, inplace=True)
#concat original row to end or start of df
newdf = pd.concat([testdf, target_row], axis=0)
Thanks
Rather than concat I would just assign directly to the df after shift
ing, then use iloc
to reference the position you want to assign the row, you have to call squeeze
so that you assign just the values and lose the original index value otherwise it'll raise a ValueError
:
In [210]:
df = pd.DataFrame({'a':np.arange(5)})
df
Out[210]:
a
0 0
1 1
2 2
3 3
4 4
In [206]:
target_row = df.ix[[2],:]
target_row
Out[206]:
a
2 2
In [211]:
df = df.shift()
df.iloc[0] = target_row.squeeze()
df
Out[211]:
a
0 2
1 0
2 1
3 2
4 3
EDIT
To insert at the 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]:
a
0 1
1 2
2 3
3 4
4 2
Another update
Thanks to @AsheKetchum for pointing out that my earlier answer is incorrect, now looking at this 3 years later I realise you could just reindex
the orig df:
If we take a copy of the index as a list
:
In[24]:
idx = df.index.tolist()
idx
Out[24]: [0, 1, 2, 3, 4]
then we can pop
the index of interest from this list:
In[25]:
idx.pop(2)
idx
Out[25]: [0, 1, 3, 4]
Now we can reindex
by prepending to this list:
In[26]:
df.reindex([2] + idx)
Out[26]:
a
2 2
0 0
1 1
3 3
4 4
Or appending:
In[27]:
df.reindex(idx+[2])
Out[27]:
a
0 0
1 1
3 3
4 4
2 2