熊猫最后一排

fx-*_*rin 9 python pandas

有没有办法获取和删除最后一行像poppython本机列表的方法?

我知道我可以在下面做.我只是想让它成为一条线.

df.ix[df.index[-1]]
df = df[:-1]
Run Code Online (Sandbox Code Playgroud)

shi*_*vsn 7

假设样本数据帧:

In[51]:df
Out[51]: 
   a  b
0  1  5
1  2  6
2  3  7
3  4  8
Run Code Online (Sandbox Code Playgroud)

你可以使用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
Run Code Online (Sandbox Code Playgroud)

或使用numpy作为np:

df,last_row=pd.Dataframe(np.delete(df.values,(-1),axis=0)),df.tail(1)
Run Code Online (Sandbox Code Playgroud)