导入csv文件时,我似乎无法设置索引。如果我正确导入文件,我无法解决问题,我现在正在解释器中做所有事情,这就是我所拥有的:
df = pd.read_csv('E:/test.vbo', sep='\t', encoding='iso-8859-1', skiprows=97)
print(df.head())
这给出了以下内容:
sats time lat long velocity heading height ...
0 [data]
1 008 144403.30 003067.21791 000031.98044 010.033 097.16 +00112.43 ...
2 008 144403.40 003067.21777 000031.98036 010.584 098.58 +00113.06 ...
3 008 144403.50 003067.21765 000031.98032 010.809 099.74 +00113.72 ...
4 008 144403.60 003067.21749 000031.98025 011.231 101.05 +00114.34 ...
5 008 144403.70 003067.21728 000031.98021 011.575 102.14 +00114.89 ...
但是,这行很好: 打印(df.set_index('时间'))
给出错误:
>>> print(df.set_index('time'))
Traceback (most recent call last):
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\indexes\base.py", line 1945, in get_loc
return self._engine.get_loc(key)
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4154)
File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4018)
File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12368)
File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12322)
KeyError: 'time'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\core\frame.py", line 2837, in set_index
level = frame[col]._values
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\core\frame.py", line 1997, in __getitem__
return self._getitem_column(key)
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\core\frame.py", line 2004, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\core\generic.py", line 1350, in _get_item_cache
values = self._data.get(item)
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\core\internals.py", line 3290, in get
loc = self.items.get_loc(item)
File "C:\Users\rob.kinsey\AppData\Local\Continuum\Anaconda3\lib\site-packages\
pandas\indexes\base.py", line 1947, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4154)
File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4018)
File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12368)
File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.g
et_item (pandas\hashtable.c:12322)
KeyError: 'time'
>>>
我错过了什么?
>>> print(df.columns.tolist())
['sats time lat long velocity heading height vert-vel dgps racceleratorpedal ast
eeringwheel pbrake glateral glongitudinal awingpitch ngearengaged nengine nwheel
fr nwheelrr nwheelfl nwheelrl mengine rdrsavailabledisplayed pwaterpump toil tdc
dc tmotorstator phvac nephsmotor pboost taircharge tcoolant tclutchoil nephspump
demanded tcellmax vbattery paerooil taerooil tmcucoldplate awingpitchdemand tmcu
_igbtmax avifileindex avitime ']
解决了,这是正确的read_csv行:
>>> df = pd.read_csv('E:/vbox_data/P1GTR__20150922144312_0001.vbo', delim_whitespace=True, encoding='iso-8859-1', header=90)