When working with large datasets, it can eat a lot of memory to read the entire dataset in pandas. You can, however, read only a few rows from the dataframe if you want to have a look at the data or do some simple analysis. In this tutorial, we’ll look at how to read only the first n rows of a CSV file to a pandas dataframe.
Read the first n rows in pandas
You can use the pandas read_csv() function to read a CSV file. To only read the first few rows, pass the number of rows you want to read to the nrows parameter. Note that, by default, the read_csv() function reads the entire CSV file as a dataframe. The following is the syntax:
df_firstn = pd.read_csv(FILE_PATH, nrows=n)
Here, FILE_PATH is the path to the CSV file you want to load and n is the number of rows you want to read from the top of the file.
Let’s look at some examples of reading just the top n rows of a CSV file in pandas. For this tutorial, we’ll be using the IMDB movie reviews dataset which you can download here. The dataset contains 50 thousand reviews from IMDB tagged positive and negative.
Here’s a snapshot of how the data looks:
Load only the top n rows
Let’s say you want to read only the first 100 rows of the dataset instead of reading the entire dataset. To read only the first 100 rows, pass 100 to the
import pandas as pd # read the top n rows of csv file as a dataframe reviews_df = pd.read_csv("IMDB Dataset.csv", nrows=100) # print the shape of the dataframe print("Dataframe shape:", reviews_df.shape)
Dataframe shape: (100, 2)
You can see that only the first 100 rows of the CSV file were read and loaded to the dataframe. Let’s print out the first five rows of the dataframe.
review sentiment 0 One of the other reviewers has mentioned that ... positive 1 A wonderful little production. <br /><br />The... positive 2 I thought this was a wonderful way to spend ti... positive 3 Basically there's a family where a little boy ... negative 4 Petter Mattei's "Love in the Time of Money" is... positive
2. Load n rows from the middle
You can also skip rows from the dataset you are reading. For example, to skip the first three reviews and then read 100 rows from the above dataset:
# read n rows of csv file after skipping rows as a dataframe reviews_df = pd.read_csv("IMDB Dataset.csv", skiprows=range(1,4), nrows=100) # print the shape of the dataframe print("Dataframe shape:", reviews_df.shape)
Dataframe shape: (100, 2)
Note that, we passed a range to the
skiprows parameter. This is because the first row of our dataset is the heading and we do not want to skip it. You can also pass an integer to the
skiprows parameter which will skip rows from the beginning. Let’s print out the first five rows of the dataframe.
review sentiment 0 Basically there's a family where a little boy ... negative 1 Petter Mattei's "Love in the Time of Money" is... positive 2 Probably my all-time favorite movie, a story o... positive 3 I sure would like to see a resurrection of a u... positive 4 This show was an amazing, fresh & innovative i... negative
You can see that the first three reviews have been skipped in this dataframe.
For more on the parameters of the read_csv() function, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Other similar tutorials: