How can I partially read a huge CSV file?

后端 未结 2 1109
北恋
北恋 2020-12-01 04:38

I have a very big csv file so that I can not read them all into the memory. I only want to read and process a few lines in it. So I am seeking a function in Pandas which cou

2条回答
  •  天涯浪人
    2020-12-01 04:59

    Use chunksize:

    for df in pd.read_csv('matrix.txt',sep=',', header = None, chunksize=1):
        #do something
    

    To answer your second part do this:

    df = pd.read_csv('matrix.txt',sep=',', header = None, skiprows=1000, chunksize=1000)
    

    This will skip the first 1000 rows and then only read the next 1000 rows giving you rows 1000-2000, unclear if you require the end points to be included or not but you can fiddle the numbers to get what you want.

提交回复
热议问题