Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last N rows of a pandas DataFrame?

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

like image 775
bigbug Avatar asked Feb 02 '13 14:02

bigbug


People also ask

How do you get the last 3 rows in pandas?

Drop Last Row of Pandas DataFrame Using head() Function You can also use df. head(df. shape[0] -1) to remove the last row of pandas DataFrame.

How do you get the last 100 rows in pandas?

Use drop() to remove last N rows of pandas dataframe In pandas, the dataframe's drop() function accepts a sequence of row names that it needs to delete from the dataframe.

How do you get n rows in pandas?

You can use df. head() to get the first N rows in Pandas DataFrame. Alternatively, you can specify a negative number within the brackets to get all the rows, excluding the last N rows.

Which function will select the last 3 rows of the data frame?

Using the tail() function, we fetched the last 3 rows of dataframe as a dataframe and then just printed it.


1 Answers

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

like image 93
Wes McKinney Avatar answered Sep 19 '22 22:09

Wes McKinney