Get the last 10000 lines of a csv file

Viewed 3597

In pandas, I can just use pandas.io.parser.read_csv("file.csv", nrows=10000) to get the first 10000 lines of a csv file.

But because my csv file is huge, and the last lines are more relevant than the first ones, I would like to read the last 10000 lines. However, this is not that easy even if I know the length of the file, because if I skip the first 990000 lines of a 1000000 line csv file using pandas.io.parser.read_csv("file.csv", nrows=10000, skiprows=990000) the first line, which contains the file header, is skipped, as well. (header=0 is measured after skiprows is applied, so it does not help either.)

How do I get the last 10000 lines from a csv file with a header in line 0, preferably without knowing the length of the file in lines?

4 Answers
Related