Pandas pop last row
<p>Is there any way to get and remove last row like <code>pop</code> method of python native list?</p>

<p>I know I can do like below. I just want to make it one line.</p>

<pre><code>df.ix[df.index[-1]]
df = df[:-1]
</code></pre>

<p>Suppose sample dataframe:</p>

<pre><code>In[51]:df
Out[51]: 
   a  b
0  1  5
1  2  6
2  3  7
3  4  8
</code></pre>

<p>you can do using <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html" rel="nofollow">df.drop</a>:</p>

<pre><code>In[52]:df,last_row=df.drop(df.tail(1).index),df.tail(1)

In[53]:df
Out[53]: 
   0  1
0  1  5
1  2  6
2  3  7

In[54]:last_row
Out[54]: 
   a  b
3  4  8
</code></pre>

<p>or using <a href="http://www.numpy.org/" rel="nofollow">numpy</a> as <code>np</code>:</p>

<pre><code>df,last_row=pd.Dataframe(np.delete(df.values,(-1),axis=0)),df.tail(1)
</code></pre>

python pandas