How can I index this csv file?

Viewed 56

I am writing code for a project that will parse METARs (hourly weather reports at airports). I am attempting to use a csv file (see the end of post). I am wondering where my code is faulty as I am getting an error. I am thinking it is something with my array but I cannot figure out where. My Code

from time import sleep
import os
import pandas as pd
os.getcwd()
os.chdir('/home/pi/wmap')

i=2
while i==2:
    n=0
    t12=pd.read_csv('mtr.csv', usecols = ['ASOS'])
    iad_metars = MetarSet('t12[n]')
    iad_metars.refresh()
    latest_iad_metars = iad_metars.get_latest()
    #print(latest_iad_metars.raw_text)
    print(latest_iad_metars.station)#prints latest city ID
    print(latest_iad_metars.temp) #runs temperature
    print(latest_iad_metars.dewpoint)
    #print(latest_iad_metars.winds)
    print(latest_iad_metars.visibility)
    #print(latest_iad_metars.cloud_layers)
    n=n+1
    print("******\n")
    sleep(15)
    if n==26:
        n=0

The error I receive is

Traceback (most recent call last):
  File "map.py", line 11, in <module>
    t12=pd.read_csv('mtr.csv', usecols = ['ASOS'])
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 688, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 454, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 948, in __init__
    self._make_engine(self.engine)
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 1180, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 2056, in __init__
    _validate_usecols_names(usecols, self.orig_names)
  File "/home/pi/.local/lib/python3.7/site-packages/pandas/io/parsers.py", line 1305, in _validate_usecols_names
    f"Usecols do not match columns, columns expected but not found: {missing}"
ValueError: Usecols do not match columns, columns expected but not found: ['ASOS']


------------------
(program exited with code: 1)
Press return to continue

CSV KCAR 0 KBOS 1 KALB 2 KJFK 3 KBWI 4 KCLT 5 KCHS 6 KATL 7 KMCO 8 KMIA 9 KMSY 10 KIAH 11 KDFW 12 KOUN 13 KOMA 14 KORD 15 KMSP 16 KCVG 17 KSTL 18 KBIS 19 KUNR 20 KABQ 21 KPHX 22 KLAS 23 KSAN 24 KSFO 25 KSEA 26

1 Answers

Your code is throwing an error on line 11, where you try to import the file mtr.csv with the pandas read_csv method and usecols param. The ValueError thrown says that the column header ASOS wasn't found.

An alternative would be to import using the default params then select your column of interest, like I mentioned in my comment above.

t12 = pd.read_csv('mtr.csv')
t12 = t12[['ASOS']]
Related