Difference between nrows and df.head() in pandas

Question:

I was going through a tutorial where he taught first df.head(25) will give you the first 25 rows of the data, then he showed pd.read_csv("__", nrows=25).

My question is what is the difference between these two options? If there is none then why there are 2 methods to get rows?

Thanks, Regards

Asked By: Abhay Banugariya

||

Answers:

df.head assumes you already have a pandas.DataFrame read into your code and stored in the variable df; you can use df.head(25) to view the first 25 rows of this DataFrame.

pandas.read_csv(filename, nrows=25) would instead read data directly from a csv file located at filename, but only its first 25 rows. This data isn’t already stored as a variable in your python instance, it is in the csv file.

Answered By: Emi OB
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.