Read the last N lines of a CSV file in Python with numpy / pandas

后端 未结 3 633
北荒
北荒 2021-01-13 18:05

Is there a quick way to read the last N lines of a CSV file in Python, using numpy or pandas?

  1. I cannot do skip_header

3条回答
  •  遥遥无期
    2021-01-13 19:00

    Use skiprows parameter of pandas read_csv(), the tougher part is finding the number of lines in the csv. here's a possible solution:

    with open('filename',"r") as f:
        reader = csv.reader(f,delimiter = ",")
        data = list(reader)
        row_count = len(data)
    
    df = pd.read_csv('filename', skiprows = row_count - N)
    

提交回复
热议问题