简体   繁体   中英

pandas pd.read_csv KeyError:2

i'm trying to read my cars sales data and transfer them to numpy array. But it do not work. here is the data image. enter image description here

import numpy as np
import pandas as pd

for i in range(2,34):
    data = pd.read_csv('Book2.csv')[i].values
data.shape

print(data)

Error message:

Traceback (most recent call last):
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2525, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\Files\python\neutral_network\2.py", line 5, in <module>
    data = pd.read_csv('Book2.csv')[i].values
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2139, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\frame.py", line 2146, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\generic.py", line 1842, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\internals.py", line 3843, in get
    loc = self.items.get_loc(item)
  File "C:\Users\ThinkPad\AppData\Local\Programs\Python\Python36\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 2

The error you are is getting because of index i in line number 5. The better way to convert your whole csv in to numpy ndarray is below.

data = pd.read_csv('Book2.csv')
numpyMatrix = data.as_matrix()

you can also try data.values to convert in to numpy ndarray but element type will be object.

As Prakash notes, the problem is in the index variable i line 5. read_csv returns a pandas dataframe and Pandas does not know what to do with the index value.

There are two other fundamental problems. First, you are re-reading the file each time through your loop and re-assigning data, so even if the code worked as you intended, you'd at best end up with a single column of data. Second, read_csv will not properly interpret your data out of the box. The issue is the commas in the second field, that pandas will initially interpret as separators, so you have to tell it to ignore commas inside quotes. I found the following to work (on a subset of your data):

In [35]: data2=pd.read_csv("Book2.csv", skipinitialspace=True, quotechar='"')

In [36]: data2
Out[36]:
   Date     H6sv   h6mi  h6shv
0     1  26, 368  17.30  18182
1     2  24, 402  18.00  15030
2     3  24, 451  30.33  11312
3     4  26, 528  60.52   9730

Followed by dropping the columns you do not want:

In [55]: data2.drop(columns="Date")
Out[55]:
      H6sv   h6mi  h6shv
0  26, 368  17.30  18182
1  24, 402  18.00  15030
2  24, 451  30.33  11312
3  26, 528  60.52   9730

Yes, it took me 55 tries to get what I wanted...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM