Home > Back-end >  Difference between nrows and df.head() in pandas
Difference between nrows and df.head() in pandas

Time:12-29

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

CodePudding user response:

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.

  • Related