Python Pandas:如何仅读取前 n 行 CSV 文件?
我有一个非常大的数据集,我无法读取整个数据集.所以,我想只读取其中的一部分进行训练,但我不知道该怎么做.任何想法将不胜感激.
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. Any thought will be appreciated.
如果您只想读取前 999,999(非标题)行:
If you only want to read the first 999,999 (non-header) rows:
read_csv(..., nrows=999999)
如果您只想读取 1,000,000 ... 1,999,999 行
If you only want to read rows 1,000,000 ... 1,999,999
read_csv(..., skiprows=1000000, nrows=999999)
nrows : int,默认 None 要读取的文件行数.对...有用读取大文件*
nrows : int, default None Number of rows of file to read. Useful for reading pieces of large files*
skiprows:类似列表或整数文件开头要跳过的行数(0-indexed)或要跳过的行数(int)
skiprows : list-like or integer Row numbers to skip (0-indexed) or number of rows to skip (int) at the start of the file
对于大文件,您可能还想使用 chunksize:
and for large files, you'll probably also want to use chunksize:
chunksize:整数,默认无返回用于迭代的 TextFileReader 对象
chunksize : int, default None Return TextFileReader object for iteration