【问题标题】:How can I read a file with text before the CSV header?如何在 CSV 标头之前读取带有文本的文件?
【发布时间】:2021-08-24 12:50:42
【问题描述】:

我正在使用这个数据库https://www.cryptodatadownload.com/cdd/Exmo_BTCEUR_1h.csv 当我尝试提取“关闭”列时出现此错误

我的代码如下:

> data= pd.read_csv('C:/Users/Downloads/btceur1h.csv')
BTC=pd.DataFrame(data)
BTC['close']

并且发生了这个错误:

> ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'close'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-50-e48080b9c83e> in <module>
----> 1 BTC['close']

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2798             if self.columns.nlevels > 1:
   2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)
   2801             if is_integer(indexer):
   2802                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'close'

我怎样才能避免这个问题??

【问题讨论】:

  • 这是一个文本文件,不是数据库。您在询问如何从 Dataframe 中读取特定列。那一栏真的存在吗?该文件是有效 CSV,还是只是使用虚假扩展名保存的文本报告?
  • 你的BTC 返回一个[28611 rows x 1 columns] 数据框

标签: python pandas dataframe keyerror


【解决方案1】:

这是一个平面文件,而不是数据库。第一行包含一个 URL 而不是字段名称:

https://www.CryptoDataDownload.com
unix,date,symbol,open,high,low,close,Volume EUR,Volume BTC
1629763200000,2021-08-24 00:00:00,BTC/EUR,42138.6,42304.29,42086.01,42219.58,5767.798367994,0.1366143
1629759600000,2021-08-23 23:00:00,BTC/EUR,42288.94,42335.66,42134.99,42185.41,7318.3523473165,0.17348065

您需要跳过第一行。 read_csv 允许您指定要跳过 skiprows 参数的行数组:

data= pd.read_csv('C:/Users/Downloads/btceur1h.csv', skiprows=[0])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多