Error reading CSV file column data in Python: list index out of range

Asked 2 years ago, Updated 2 years ago, 17 views

I'd like to ask you about reading the csv file.

import csv
    
f = open("test2.csv", "r", )
reader=csv.reader(f)
    
For row in reader:
    print(row)
    
f.close() 

We are currently loading the csv file in this way and successfully loading it.

From here, I would like to count how many numbers there are in column E.
So,

import csv
f = open("test2.csv", "r", )
reader=csv.reader(f)
count = 0
For row in reader:
    colE=int(row[5])
    if colE>=10:
        count+=1
        print(count)
        
f.close()

The error "list index out of range" occurred.Please let me know what this means and if there is anything wrong with it.

The csv file looks like this

Downloaded time: October 16, 2019 12:01:46                  
                    
staying overnight at a temple
Wind direction (m/s) per hour (month/day)
                    
2018 2 12 11 10.1 Northwest
2018 2 12 2 9.1 West
2018 2 12 3 10 West
2018 2 12 4 11.4 West
2018 2 125 10.4 West
2018 2 12 6 10.8 West
2018 2 12 7 11.1 West
2018 2 12 8 12.4 West
2018 2 12 9 11.5 Northwest
2018 2 12 10 11.2 West
2018 2 12 11 8.9 West-northwest
2018 2 12 12 8.1 West-northwest
2018 2 12 13 5.7 West-northwest
2018 2 12 14 6.3 West-West
2018 2 12 15 5.5 West-West
2018 2 12 16 7.1 West-West
2018 2 12 17 7 West-West
2018 2 12 18 6.4 West-West
2018 2 12 198 West-northwest
2018 2 12 20 8.6 West-northwest
2018 2 12 21 7.9 West-northwest
2018 2 12 22 8.7 West-West
2018 2 12 23 8.9 West
2018 2 12 24 10.1 West
2018 2 13 15.2 Northwest
2018 2 13 27 West-northwest
2018 2 13 3 6.4 West-West
2018 2 13 4 5.4 Northwest
2018 21355.4 Northwest
2018 2 13 6 3.9 Northwest
2018 2 13 7 3.5 Northwest
2018 2 13 8 1.2 North
2018 2 13 93 Northwest
2018 213 10 3.4 Northwest
2018 2 13 11 3.1 Northwest
2018 213 124 Northwest
2018 2 13 13 1.9 Northwest
2018 2 13 14 2.2 Northwest
2018 213 15 2.1 Northwest
2018 213 16 2.7 Northwest
2018 2 13 17 2.3 West-West
2018 2 13 180.5 West
2018 2 13 19 4.9 West-northwest
2018 2 13 20 9.9 West
2018 2 13 21 11.1 West
2018 2 13 22 8.3 West-West
2018 2 13 23 10.1 West
2018 2 13 24 7.7 West

python

2022-09-30 16:39

2 Answers

list index out of range error

Does this refer to IndexError? (Error should be pasted correctly)

IndexError is sent when a reference is made to an out-of-range element during sequence access.

Embedded Exceptions—Python 3.8.0 Documentation

In the code listed, the only access to the sequence is colE=int(row[5]) in line 6, so there seems to be a problem when accessing it here.

Now, how do I fix this code?

  • First of all, the subscript corresponding to column E is 4, so correct it (if it is 5, the west-northwest etc. should be a hit)
  • Try
  • print(len(row)) to see if any strange values are loaded
  • iflen(row)!=5:sys.exit(), re-create the column to exit if it finds an odd length column, such as

and so on.


2022-09-30 16:39

For your information, here's how pandas.read_csv() works.

read_csv() has a parameter called skiprows that allows you to specify the number of lines to skip the read.For the CSV data above, specify skiprows=3 because the first three lines are not required and the fourth line is read as a header.read_csv() ignores empty lines by default (skip_blank_lines=True).

import pandas as pd

df = pd.read_csv('test2.csv', skiprows=3)
count=df[df['wind speed (m/s)']>=10.0].shape[0]

print(count)

add

If there is a cell with no value in column E on the way, an error will appear and it will stop.

For example, there are lines like this.

2018, 2, 12, 6, West

In this case, check whether column E (wind speed) has a value in for loop, and if there is no value, check the next row of data.

import csv

f = open("test2.csv", "r")
reader=csv.reader(f)
[ next(reader) for_in range(5)]

count = 0
For row in reader:
  if not row[4].strip():#confirm if column E has no value
    continue
  colE=float(row[4])
  if colE>=10:
    count+=1

print(count)
f.close()

Incidentally, in the case of Pandas data frames, the blanks will be treated as NaN.


2022-09-30 16:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.