gpt4 book ai didi

python - 如何在 python 中将行标题附加到数据框。行名称应取自字典

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:53 24 4
gpt4 key购买 nike

我有一个像这样的数据框:

      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

我想通过读取字典字典的 a 值来附加上述数据框的行名称

字典:

 [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 数组(矩阵形式)

最佳答案

您需要先转换为列表:

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]

关于python - 如何在 python 中将行标题附加到数据框。行名称应取自字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45584171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com