3

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

  • Possible duplicate of [How to read a 6 GB csv file with pandas](https://stackoverflow.com/questions/25962114/how-to-read-a-6-gb-csv-file-with-pandas) – Lucas Sep 03 '18 at 15:16
  • Possible duplicate of [Python Pandas read\_csv skip rows but keep header](https://stackoverflow.com/questions/27325652/python-pandas-read-csv-skip-rows-but-keep-header) - assume your issue is that it's reading rows but skipping the header – asongtoruin Sep 03 '18 at 15:16

1 Answers1

4

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])
N. Fatma
  • 221
  • 1
  • 3
  • 1
    My file only has a one column I use those code: dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , skiprows=60, nrows=75, names=[0]) dts = pd.read_csv('C:\DtsPMU\dts.csv', dtype=float , skiprows=60, nrows=75, names=[1]) but doesn't work. – Juan Jose Quiroz Moreno Sep 03 '18 at 16:37
  • Can you show the output or error, whatever you are getting? – N. Fatma Sep 03 '18 at 17:42