pandas 熊猫弹出最后一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39263411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:55:49 来源:igfitidea点击:
Pandas pop last row
提问by fx-kirin
Is there any way to get and remove last row like pop
method of python native list?
有没有办法像pop
python本机列表的方法一样获取和删除最后一行?
I know I can do like below. I just want to make it one line.
我知道我可以像下面那样做。我只想让它成为一行。
df.ix[df.index[-1]]
df = df[:-1]
回答by shivsn
Suppose sample dataframe:
假设示例数据帧:
In[51]:df
Out[51]:
a b
0 1 5
1 2 6
2 3 7
3 4 8
you can do using df.drop:
你可以使用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
or using numpyas np
:
或使用numpy作为np
:
df,last_row=pd.Dataframe(np.delete(df.values,(-1),axis=0)),df.tail(1)