import pandas as pd
import numpy as np
df = pd.read_csv('~/test.txt')
list(df.columns.values)
bekomme ich folgende Ausgabe:Erstellen numpy Array von Pandas Datenrahmen
['time', 'Res_fs1', 'angle1', 'Res_fs2', 'angle2', 'Res_ps1', 'Force1',
'Res_ps2', 'Force2', 'object']
wenn ich versuche, eine numppy Array mit Res_fs1, Res_fs2, Res_ps1, Res_ps2
X=np.array(df['Res_fs1','Res_fs2','Res_ps1','Res_ps2'])
ich zu erstellen diese Fehlermeldung sagt Schlüsselfehler, obwohl die Schlüssel vorhanden sind:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1969, in
__getitem__
return self._getitem_column(key)
File "/usr/lib/python2.7/dist-packages/pandas/core/frame.py", line 1976, in
_getitem_column
return self._get_item_cache(key)
File "/usr/lib/python2.7/dist-packages/pandas/core/generic.py", line 1091,
in _get_item_cache
values = self._data.get(item)
File "/usr/lib/python2.7/dist-packages/pandas/core/internals.py", line 3211,
in get
loc = self.items.get_loc(item)
File "/usr/lib/python2.7/dist-packages/pandas/core/index.py", line 1759, in
get_loc
return self._engine.get_loc(key)
File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc
(pandas/index.c:3979)
File "pandas/index.pyx", line 157, in pandas.index.IndexEngine.get_loc
(pandas/index.c:3843)
File "pandas/hashtable.pyx", line 668, in
pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12265)
File "pandas/hashtable.pyx", line 676, in
pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12216)
KeyError: ('Res_fs1', 'Res_fs2', 'Res_ps1', 'Res_ps2')
Mögliches Duplikat von https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array-preserving-index. Verwende 'as_matrix' oder' to_records'. – aleneum