Reading just range of rows from one csv file in Python using pandas

Viewed 7419

I'm trining to read a csv file with Python and Pandas, but my file has a big size (1 GB) so I can't to read all datas. By this web site I learned to use nrows to read rows from my file, for example read first 75 rows, but I can't to read a range of rows.

dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , nrows=75)

This link Python Pandas reads_csv skip first x and last y rows talks to use a code like this:

dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , skiprows=60, nrows=75)

Whit that code I'm try to read just range de rows (start in 60 to 75) but it doen't work.

How could I read range of rows from my csv file?

I'm use Python 3.6.5 and Pandas 0.23.2

1 Answers

This code works fine

dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , skiprows=60, nrows=75)

The only problem is that it makes the row number 60 as the header, if you want the original header then use

names : array-like, default None List of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list will cause a UserWarning to be issued.

For example: if your file has 3 columns, then

dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , skiprows=60, nrows=75, names=[0,1,2])
Related