0

I'm given a .csv file, which is a data file from a weather station that includes date, time, temperature, dewpoint, humidity, etc. This is what I have so far in a Python file:

import math
import pandas as pd
import math
import numpy
openfile=pd.read_csv('KOAK.csv','r',delimiter=',',skiprows=8,header=None)
f1=openfile.read()
openfile.close()

I am skipping the first 8 rows as these contain header information. How can I fix this Python code to read the entire data set but excluding the headers? I got the following error messages:

File "mesowest3.py", line 7, in <module>
    f1=openfile.read()
  File "/swdepot/anaconda3-2019.10/lib/python3.7/site-packages/pandas/core/generic                                                                                      .py", line 5179, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'read'
Stefan
  • 37
  • 5
  • `pd.read_csv` returns a dataframe with the data of `KOAK.csv`. Take a look at [docs](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html) – DOOM Apr 10 '20 at 02:41

1 Answers1

0

read_csv reads the CSV as is into a DataFrame. So, openfile is an object of type DataFrame, which does not have the function read() as your error says.

AttributeError: 'DataFrame' object has no attribute 'read'

type openfile.head() to see your DataFrame in your environment. You'll see the DataFrame exists and you can start operating on it!

Hope that helps!

zerecees
  • 697
  • 4
  • 13
  • So I should add openfile.head() below my openfile= line? Thank you for the info! – Stefan Apr 10 '20 at 02:55
  • Correct you are sir! If you're in a Jupyter environment, it will print nicely. If you're in a terminal environment, it will be legible, but old school cool. – zerecees Apr 10 '20 at 02:57
  • It didn't seem to work unfortunately, the error says: AttributeError: 'DataFrame' object has no attribute 'close' – Stefan Apr 10 '20 at 03:28
  • Take out `openfile.close()` too :) – zerecees Apr 10 '20 at 03:37