1
採取我有這樣一個數據幀:如何將行標題附加到python中的數據框。行的名稱應該從字典
1 2 3
0.950389665 0.29695614 0.250323227
0.228821863 0.544082251 0.809445825
0.595764836 0.726256844 0.301979059
0.12775065 0.307534453 0.7791458
0.538780306 0.651055165 0.8450824
0.674011952 0.03239639 0.650357821
0.257926954 0.828308299 0.526425688
我想通過讀字典詞典的價值
字典附上行名稱爲上述數據幀:
[dict_values(['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh'])]
這是我使用的代碼:
Result_matrix = df.set_index([dictionary.values()])
不過,我得到以下錯誤:
Traceback (most recent call last):
File "<ipython-input-79-dc4a38b0a2d5>", line 1, in <module>
enrond_matrix = enrond_dataframe.set_index([ reverse_dictionary.values()])
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\frame.py", line 2830, in set_index
level = frame[col]._values
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
return self._getitem_column(key)
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
values = self._data.get(item)
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\internals.py", line 3590, in get
loc = self.items.get_loc(item)
File "C:\Users\30295\AppData\Local\Continuum\Anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 156, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5210)
請幫助我。
如果可能的話我希望有最終的結果作爲numpy的陣列(矩陣形式)
@jezreal - 這一直奏效。非常感謝您的幫助 –
很高興能爲您提供幫助,祝您好運! – jezrael