Python Pandas: How to read only first n rows of CSV files in?

后端 未结 1 648
误落风尘
误落风尘 2020-12-04 09:34

I have a very large data set and I can\'t afford to read the entire data set in. So, I\'m thinking of reading only one chunk of it to train but I have no idea how to do it.

相关标签:
1条回答
  • 2020-12-04 09:55

    If you only want to read the first 999,999 (non-header) rows:

    read_csv(..., nrows=999999)
    

    If you only want to read rows 1,000,000 ... 1,999,999

    read_csv(..., skiprows=1000000, nrows=999999)
    

    nrows : int, default None Number of rows of file to read. Useful for reading pieces of large files*

    skiprows : list-like or integer Row numbers to skip (0-indexed) or number of rows to skip (int) at the start of the file

    and for large files, you'll probably also want to use chunksize:

    chunksize : int, default None Return TextFileReader object for iteration

    pandas.io.parsers.read_csv documentation

    0 讨论(0)
提交回复
热议问题