I have a very large CSV file, and my memory can't read the whole CSV at all. How can I get the first 100 lines of this CSV without reading all the data
Asked
Active
Viewed 1,322 times
-2
-
2There's an option `nrows` in `pd.read_csv`. You can try `pd.read_csv('file.csv', nrows=100)`. – Quang Hoang May 17 '21 at 01:42
-
Does this answer your question? [Python Pandas: How to read only first n rows of CSV files in?](https://stackoverflow.com/questions/23853553/python-pandas-how-to-read-only-first-n-rows-of-csv-files-in) – Derek O May 17 '21 at 17:48
1 Answers
1
import pandas as pd
first100 = pd.read_csv("someCoolData.csv", nrows=100)
This will read only the first 100 rows into a data frame from a given csv file.
For further explanation of what's going on refer to: https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html

Tristan Zaleski
- 34
- 2
-
Thank you. I also considered this method at the beginning, but I can't be sure that panda reads the specified number of rows after reading the CSV into memory. I just tested this situation, and I found that panda only reads the specified number of rows into memory, not all of them. – thewholeworld May 17 '21 at 03:19