Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-将Dataframe转换为键所在的字典(行索引、列名)_Python_Pandas_Dictionary - Fatal编程技术网

Python-将Dataframe转换为键所在的字典(行索引、列名)

Python-将Dataframe转换为键所在的字典(行索引、列名),python,pandas,dictionary,Python,Pandas,Dictionary,我想将数据帧转换为字典,其中键是数据帧的行索引和列名,每个键的值是每个行索引和列名的位置 假设这是我的数据帧 1 2 3 0 1 2 3 1 4 5 6 2 7 8 9 我希望我的字典看起来像这样: ((0, 1), 1) ((0, 2), 2) ((0, 3), 3) ((1, 1), 4) 如何实现这一点?用于多索引系列: 但是如果想要tuples: s = df.stack() L = list(zip(s.index.tolist(), s)) print (L) [((0,

我想将数据帧转换为字典,其中键是数据帧的行索引和列名,每个键的值是每个行索引和列名的位置

假设这是我的数据帧

  1 2 3 
0 1 2 3
1 4 5 6
2 7 8 9
我希望我的字典看起来像这样:

((0, 1), 1)
((0, 2), 2)
((0, 3), 3)
((1, 1), 4)
如何实现这一点?

用于
多索引系列

但是如果想要
tuple
s:

s = df.stack()
L = list(zip(s.index.tolist(), s))
print (L)
[((0, 1), 1), ((0, 2), 2), ((0, 3), 3), 
 ((1, 1), 4), ((1, 2), 5), ((1, 3), 6),
 ((2, 1), 7), ((2, 2), 8), ((2, 3), 9)]
用于
多索引系列
,包括:

但是如果想要
tuple
s:

s = df.stack()
L = list(zip(s.index.tolist(), s))
print (L)
[((0, 1), 1), ((0, 2), 2), ((0, 3), 3), 
 ((1, 1), 4), ((1, 2), 5), ((1, 3), 6),
 ((2, 1), 7), ((2, 2), 8), ((2, 3), 9)]

你想要字典还是元组?@harvpan我想元组可以工作。你想要字典还是元组?@harvpan我想元组可以工作too@ClarityParity-不客气!如果我的答案有帮助,别忘了——点击答案旁边的复选标记,将其从灰色变为填充。谢谢。谢谢你的快速回复!我本来打算在20分钟后投票并接受的,但一路上我却被冷落了(@ClarityParity-不客气!如果我的答案有帮助,别忘了-单击答案旁边的复选标记,将其从灰色变为已填写。谢谢。感谢您的快速回复!我本想在20分钟后进行投票并接受,但一路上被忽略了:(