Way to read first few lines for pandas dataframe

前端 未结 1 1283
抹茶落季
抹茶落季 2020-12-13 03:31

Is there a built-in way to use read_csv to read only the first n lines of a file without knowing the length of the lines ahead of time? I have a la

相关标签:
1条回答
  • 2020-12-13 04:01

    I think you can use the nrows parameter. From the docs:

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

    which seems to work. Using one of the standard large test files (988504479 bytes, 5344499 lines):

    In [1]: import pandas as pd
    
    In [2]: time z = pd.read_csv("P00000001-ALL.csv", nrows=20)
    CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
    Wall time: 0.00 s
    
    In [3]: len(z)
    Out[3]: 20
    
    In [4]: time z = pd.read_csv("P00000001-ALL.csv")
    CPU times: user 27.63 s, sys: 1.92 s, total: 29.55 s
    Wall time: 30.23 s
    
    0 讨论(0)
提交回复
热议问题