【问题标题】:Read an entire data set but excluding the headers?读取整个数据集但不包括标题?
【发布时间】:2020-07-22 18:48:33
【问题描述】:

我得到了一个 .csv 文件,它是来自气象站的数据文件,其中包括日期、时间、温度、露点、湿度等。这是我目前在 Python 文件中所拥有的:

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()

我将跳过前 8 行,因为它们包含标题信息。如何修复此 Python 代码以读取整个数据集但不包括标题?我收到以下错误消息:

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'

【问题讨论】:

  • pd.read_csv 返回一个带有KOAK.csv 数据的数据框。看看docs

标签: python pandas dataset weather readlines


【解决方案1】:

read_csv 将 CSV 原样读取到 DataFrame 中。所以,openfile 是一个 DataFrame 类型的对象,它没有你的错误所说的函数 read()

AttributeError: 'DataFrame' 对象没有属性 'read'

键入 openfile.head() 以查看您的环境中的 DataFrame。您会看到 DataFrame 存在,您可以开始对其进行操作!

希望有帮助!

【讨论】:

  • 所以我应该在我的 openfile= 行下面添加 openfile.head() ?谢谢你的信息!
  • 说得对,先生!如果您在 Jupyter 环境中,它会很好地打印。如果您在终端环境中,它将清晰易读,但很老派。
  • 不幸的是它似乎没有工作,错误说:AttributeError:'DataFrame' object has no attribute 'close'
  • 也拿出openfile.close() :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 2011-10-07
相关资源
最近更新 更多