本文介绍了如何获取pandas DataFrame的最后N行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有熊猫数据帧 df1df2(df1 是 vanila 数据帧,df2 由 'STK_ID' & 'RPT_Date' 索引):

>>>df1STK_ID RPT_Date T关闭销售折扣0 000568 20060331 3.69 5.975 NaN1 000568 20060630 9.14 10.143 NaN2 000568 20060930 9.49 13.854 NaN3 000568 20061231 15.84 19.262 NaN4 000568 20070331 17.00 6.803 NaN5 000568 20070630 26.31 12.940 NaN6 000568 20070930 39.12 19.977 NaN7 000568 20071231 45.94 29.269 NaN8 000568 20080331 38.75 12.668 NaN9 000568 20080630 30.09 21.102 NaN10 000568 20080930 26.00 30.769 NaN>>>df2TClose 销售折扣 net_sales cogsSTK_ID RPT_Date000568 20060331 3.69 5.975 NaN 5.975 2.59120060630 9.14 10.143 NaN 10.143 4.36320060930 9.49 13.854 NaN 13.854 5.90120061231 15.84 19.262 NaN 19.262 8.40720070331 17.00 6.803 NaN 6.803 2.81520070630 26.31 12.940 NaN 12.940 5.41820070930 39.12 19.977 NaN 19.977 8.45220071231 45.94 29.269 NaN 29.269 12.60620080331 38.75 12.668 NaN 12.668 3.95820080630 30.09 21.102 NaN 21.102 7.431

我可以通过以下方式获取 df2 的最后 3 行:

>>>df2.ix[-3:]TClose 销售折扣 net_sales cogsSTK_ID RPT_Date000568 20071231 45.94 29.269 NaN 29.269 12.60620080331 38.75 12.668 NaN 12.668 3.95820080630 30.09 21.102 NaN 21.102 7.431

while df1.ix[-3:] 给出所有行:

>>>df1.ix[-3:]STK_ID RPT_Date T关闭销售折扣0 000568 20060331 3.69 5.975 NaN1 000568 20060630 9.14 10.143 NaN2 000568 20060930 9.49 13.854 NaN3 000568 20061231 15.84 19.262 NaN4 000568 20070331 17.00 6.803 NaN5 000568 20070630 26.31 12.940 NaN6 000568 20070930 39.12 19.977 NaN7 000568 20071231 45.94 29.269 NaN8 000568 20080331 38.75 12.668 NaN9 000568 20080630 30.09 21.102 NaN10 000568 20080930 26.00 30.769 NaN

为什么?如何获取 df1 的最后 3 行(没有索引的数据帧)?熊猫 0.10.1

解决方案

不要忘记 DataFrame.tail!例如df1.tail(10)

I have pandas dataframe df1 and df2 (df1 is vanila dataframe, df2 is indexed by 'STK_ID' & 'RPT_Date') :

>>> df1
    STK_ID  RPT_Date  TClose   sales  discount
0   000568  20060331    3.69   5.975       NaN
1   000568  20060630    9.14  10.143       NaN
2   000568  20060930    9.49  13.854       NaN
3   000568  20061231   15.84  19.262       NaN
4   000568  20070331   17.00   6.803       NaN
5   000568  20070630   26.31  12.940       NaN
6   000568  20070930   39.12  19.977       NaN
7   000568  20071231   45.94  29.269       NaN
8   000568  20080331   38.75  12.668       NaN
9   000568  20080630   30.09  21.102       NaN
10  000568  20080930   26.00  30.769       NaN

>>> df2
                 TClose   sales  discount  net_sales    cogs
STK_ID RPT_Date
000568 20060331    3.69   5.975       NaN      5.975   2.591
       20060630    9.14  10.143       NaN     10.143   4.363
       20060930    9.49  13.854       NaN     13.854   5.901
       20061231   15.84  19.262       NaN     19.262   8.407
       20070331   17.00   6.803       NaN      6.803   2.815
       20070630   26.31  12.940       NaN     12.940   5.418
       20070930   39.12  19.977       NaN     19.977   8.452
       20071231   45.94  29.269       NaN     29.269  12.606
       20080331   38.75  12.668       NaN     12.668   3.958
       20080630   30.09  21.102       NaN     21.102   7.431

I can get the last 3 rows of df2 by:

>>> df2.ix[-3:]
                 TClose   sales  discount  net_sales    cogs
STK_ID RPT_Date
000568 20071231   45.94  29.269       NaN     29.269  12.606
       20080331   38.75  12.668       NaN     12.668   3.958
       20080630   30.09  21.102       NaN     21.102   7.431

while df1.ix[-3:] give all the rows:

>>> df1.ix[-3:]
    STK_ID  RPT_Date  TClose   sales  discount
0   000568  20060331    3.69   5.975       NaN
1   000568  20060630    9.14  10.143       NaN
2   000568  20060930    9.49  13.854       NaN
3   000568  20061231   15.84  19.262       NaN
4   000568  20070331   17.00   6.803       NaN
5   000568  20070630   26.31  12.940       NaN
6   000568  20070930   39.12  19.977       NaN
7   000568  20071231   45.94  29.269       NaN
8   000568  20080331   38.75  12.668       NaN
9   000568  20080630   30.09  21.102       NaN
10  000568  20080930   26.00  30.769       NaN

Why ? How to get the last 3 rows of df1 (dataframe without index) ?Pandas 0.10.1

解决方案

Don't forget DataFrame.tail! e.g. df1.tail(10)

这篇关于如何获取pandas DataFrame的最后N行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:13