Get the Last n Rows of a Pandas DataFrame

Your goal

You need the last n rows of a Pandas DataFrame.

Step-by-step tutorial

We can accomplish this using the DataFrame's tail() method. By default, the tail() method returns 5 rows:

>>> import pandas as pd
>>> crime_df = pd.read_csv('crime_sf_2016.csv')
>>> crime_df
        IncidntNum        Category                                           Descript DayOfWeek                    Date  ...                   Address           X          Y                               Location            PdId
0        120058272     WEAPON LAWS                          POSS OF PROHIBITED WEAPON    Friday  01/29/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  12005827212120
1        120058272     WEAPON LAWS     FIREARM, LOADED, IN VEHICLE, POSSESSION OR USE    Friday  01/29/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  12005827212168
2        141059263        WARRANTS                                     WARRANT ARREST    Monday  04/25/2016 12:00:00 AM  ...     KEITH ST / SHAFTER AV -122.388856  37.729981  (37.7299809672996, -122.388856204292)  14105926363010
3        160013662    NON-CRIMINAL                                      LOST PROPERTY   Tuesday  01/05/2016 12:00:00 AM  ...    JONES ST / OFARRELL ST -122.412971  37.785788  (37.7857883766888, -122.412970537591)  16001366271000
4        160002740    NON-CRIMINAL                                      LOST PROPERTY    Friday  01/01/2016 12:00:00 AM  ...      16TH ST / MISSION ST -122.419672  37.765050  (37.7650501214668, -122.419671780296)  16000274071000
...            ...             ...                                                ...       ...                     ...  ...                       ...         ...        ...                                    ...             ...
150495   161061000         ASSAULT                                            BATTERY    Friday  12/30/2016 12:00:00 AM  ...       OAK ST / STANYAN ST -122.453982  37.771428  (37.7714278595913, -122.453981622365)  16106100004134
150496   176000742    NON-CRIMINAL                                      LOST PROPERTY    Friday  12/30/2016 12:00:00 AM  ...   JACKSON ST / SANSOME ST -122.401857  37.796626  (37.7966261239618, -122.401857374739)  17600074271000
150497   176000758   LARCENY/THEFT                            PETTY THEFT OF PROPERTY  Thursday  12/29/2016 12:00:00 AM  ...       PINE ST / TAYLOR ST -122.412269  37.790673   (37.7906727649886, -122.41226909106)  17600075806372
150498   176000764   LARCENY/THEFT                            GRAND THEFT OF PROPERTY    Friday  12/30/2016 12:00:00 AM  ...  200 Block of STOCKTON ST -122.406659  37.788275  (37.7882745285785, -122.406658711008)  17600076406374
150499   179002868  OTHER OFFENSES  FRAUDULENT GAME OR TRICK, OBTAINING MONEY OR P...    Friday  12/02/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  17900286809024

[150500 rows x 13 columns]
>>> crime_df.tail()
        IncidntNum        Category                                           Descript DayOfWeek                    Date  ...                   Address           X          Y                               Location            PdId
150495   161061000         ASSAULT                                            BATTERY    Friday  12/30/2016 12:00:00 AM  ...       OAK ST / STANYAN ST -122.453982  37.771428  (37.7714278595913, -122.453981622365)  16106100004134
150496   176000742    NON-CRIMINAL                                      LOST PROPERTY    Friday  12/30/2016 12:00:00 AM  ...   JACKSON ST / SANSOME ST -122.401857  37.796626  (37.7966261239618, -122.401857374739)  17600074271000
150497   176000758   LARCENY/THEFT                            PETTY THEFT OF PROPERTY  Thursday  12/29/2016 12:00:00 AM  ...       PINE ST / TAYLOR ST -122.412269  37.790673   (37.7906727649886, -122.41226909106)  17600075806372
150498   176000764   LARCENY/THEFT                            GRAND THEFT OF PROPERTY    Friday  12/30/2016 12:00:00 AM  ...  200 Block of STOCKTON ST -122.406659  37.788275  (37.7882745285785, -122.406658711008)  17600076406374
150499   179002868  OTHER OFFENSES  FRAUDULENT GAME OR TRICK, OBTAINING MONEY OR P...    Friday  12/02/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  17900286809024

[5 rows x 13 columns]

We can however pass in the desired number of rows to override the default:

>>> crime_df.tail(10)
        IncidntNum         Category                                           Descript DayOfWeek                    Date  ...                   Address           X          Y                               Location            PdId
150490   160661158          ASSAULT                                            BATTERY   Tuesday  08/16/2016 12:00:00 AM  ...    1300 Block of EVANS AV -122.382742  37.739945  (37.7399451834026, -122.382742108888)  16066115804134
150491   160801508          ASSAULT                                            BATTERY    Sunday  10/02/2016 12:00:00 AM  ...       200 Block of 8TH ST -122.411272  37.776033  (37.7760329501244, -122.411271940537)  16080150804134
150492   160823994          ASSAULT                                            BATTERY    Sunday  10/09/2016 12:00:00 AM  ...      18TH ST / MISSION ST -122.419360  37.761836  (37.7618358012376, -122.419360352761)  16082399404134
150493   160858327          ASSAULT                                            BATTERY    Friday  10/21/2016 12:00:00 AM  ...     3400 Block of 18TH ST -122.419749  37.761867  (37.7618672648972, -122.419748993977)  16085832704134
150494   170302853  SECONDARY CODES                                  DOMESTIC VIOLENCE  Thursday  09/01/2016 12:00:00 AM  ...     1600 Block of 25TH ST -122.395082  37.752452  (37.7524520499536, -122.395081830837)  17030285315200
150495   161061000          ASSAULT                                            BATTERY    Friday  12/30/2016 12:00:00 AM  ...       OAK ST / STANYAN ST -122.453982  37.771428  (37.7714278595913, -122.453981622365)  16106100004134
150496   176000742     NON-CRIMINAL                                      LOST PROPERTY    Friday  12/30/2016 12:00:00 AM  ...   JACKSON ST / SANSOME ST -122.401857  37.796626  (37.7966261239618, -122.401857374739)  17600074271000
150497   176000758    LARCENY/THEFT                            PETTY THEFT OF PROPERTY  Thursday  12/29/2016 12:00:00 AM  ...       PINE ST / TAYLOR ST -122.412269  37.790673   (37.7906727649886, -122.41226909106)  17600075806372
150498   176000764    LARCENY/THEFT                            GRAND THEFT OF PROPERTY    Friday  12/30/2016 12:00:00 AM  ...  200 Block of STOCKTON ST -122.406659  37.788275  (37.7882745285785, -122.406658711008)  17600076406374
150499   179002868   OTHER OFFENSES  FRAUDULENT GAME OR TRICK, OBTAINING MONEY OR P...    Friday  12/02/2016 12:00:00 AM  ...    800 Block of BRYANT ST -122.403405  37.775421   (37.775420706711, -122.403404791479)  17900286809024

[10 rows x 13 columns]