2017-08-09 40 views
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的陣列(矩陣形式)

回答

1

你需要轉換爲list第一:

Result_matrix = df.set_index([list(d.values())]) 
print (Result_matrix) 
       1   2   3 
First 0.950390 0.296956 0.250323 
Second 0.228822 0.544082 0.809446 
Third 0.595765 0.726257 0.301979 
Fourth 0.127751 0.307534 0.779146 
Fifth 0.538780 0.651055 0.845082 
Sixth 0.674012 0.032396 0.650358 
Seventh 0.257927 0.828308 0.526426 

或分配輸出:

df.index = d.values() 
print (df) 
       1   2   3 
First 0.950390 0.296956 0.250323 
Second 0.228822 0.544082 0.809446 
Third 0.595765 0.726257 0.301979 
Fourth 0.127751 0.307534 0.779146 
Fifth 0.538780 0.651055 0.845082 
Sixth 0.674012 0.032396 0.650358 
Seventh 0.257927 0.828308 0.526426 

最後爲numpy陣列添加values,但有關行名稱的信息丟失:

print (Result_matrix.values) 
[[ 0.95038966 0.29695614 0.25032323] 
[ 0.22882186 0.54408225 0.80944582] 
[ 0.59576484 0.72625684 0.30197906] 
[ 0.12775065 0.30753445 0.7791458 ] 
[ 0.53878031 0.65105517 0.8450824 ] 
[ 0.67401195 0.03239639 0.65035782] 
+1

@jezreal - 這一直奏效。非常感謝您的幫助 –

+0

很高興能爲您提供幫助,祝您好運! – jezrael

相關問題