Get the First n Rows of a Pandas DataFrame

Your goal

You need the first n rows of a Pandas DataFrame.

Step-by-step tutorial

We can accomplish this using the DataFrame's head() method. By default, the head() 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.head()
   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

[5 rows x 13 columns]

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

          >>> crime_df.head(10)
   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
5   160002869         ASSAULT                                         BATTERY    Friday  01/01/2016 12:00:00 AM  ...    1700 Block of BUSH ST -122.426077  37.788019   (37.788018555829, -122.426077177375)  16000286904134
6   160003130  OTHER OFFENSES                                PAROLE VIOLATION  Saturday  01/02/2016 12:00:00 AM  ...      MARY ST / HOWARD ST -122.405721  37.780879  (37.7808789360214, -122.405721454567)  16000313026150
7   160003259    NON-CRIMINAL                                     FIRE REPORT  Saturday  01/02/2016 12:00:00 AM  ...     200 Block of EDDY ST -122.411778  37.783981  (37.7839805592634, -122.411778295992)  16000325968000
8   160003970        WARRANTS                                  WARRANT ARREST  Saturday  01/02/2016 12:00:00 AM  ...        4TH ST / BERRY ST -122.393357  37.775788  (37.7757876218293, -122.393357241451)  16000397063010
9   160003641  MISSING PERSON                                    FOUND PERSON    Friday  01/01/2016 12:00:00 AM  ...  100 Block of CAMERON WY -122.387182  37.720967  (37.7209669615499, -122.387181635995)  16000364175000

[10 rows x 13 columns]