【发布时间】:2021-02-20 07:23:10
【问题描述】:
我是一个新的 python 用户,我正在尝试编写一个脚本来执行一些数据整理活动。它将用于获取 .csv 文件并返回一些必要的输出。脚本如下所示:
import pandas as pd
#Basic day granularity of .csv file (day)
dfg = pd.read_csv('Henry_Hub_Natural_Gas_Spot_Price.csv', skiprows=4)
dfg.index = pd.to_datetime(dfg["Day"],format='%d/%m/%Y')
dfg.to_csv('gas-details_day.csv', index=False)
#Other granularities and sections of the .csv file (month)
dfg_month = ddfg['Henry Hub Natural Gas Spot Price Dollars per Million Btu'].resample('M').sum()
df = pd.DataFrame(dfg_month, index=dfg_month.index.strftime("%d/%m/%Y"))
df.to_csv('gas-details_month.csv', index=True)
然后我尝试使用命令python3 hello.py 运行脚本,然后错误显示如下:
Traceback (most recent call last):
File "/home/samuel/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Day'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "hello.py", line 6, in <module>
dfg.index = pd.to_datetime(dfg["Day"],format='%m/%d/%Y')
File "/home/samuel/.local/lib/python3.8/site-packages/pandas/core/frame.py", line 3024, in __getitem__
indexer = self.columns.get_loc(key)
File "/home/samuel/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: 'Day'
请帮忙,我真的很感激。
【问题讨论】:
-
我必须没有列名“Day”。
-
重新检查您的数据框,因为没有列名“Day”......也许它的列名是“day”
-
我检查了这里的数据集:github.com/Levantado/henry_hub_natural_gas_spot_price-/blob/…,你的数据集是一样的吗?检查数据集中的第 5 行,如果它有一个列名
Day -
@RishabhKumar 数据集不一样,这里是我的数据集eia.gov/dnav/ng/hist/rngwhhdm.htm
标签: python python-3.x pandas dataframe csv