pandas 弹出最后一行 [英] Pandas pop last row
本文介绍了 pandas 弹出最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以获取和删除最后一行,例如python native list的pop
方法吗?
Is there any way to get and remove last row like pop
method of python native list?
我知道我可以做到以下几点.我只想使其一行.
I know I can do like below. I just want to make it one line.
df.ix[df.index[-1]]
df = df[:-1]
推荐答案
假设示例数据框:
In[51]:df
Out[51]:
a b
0 1 5
1 2 6
2 3 7
3 4 8
您可以使用 df.drop :
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
或使用 numpy 作为np
:
df,last_row=pd.Dataframe(np.delete(df.values,(-1),axis=0)),df.tail(1)
这篇关于 pandas 弹出最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文