繁体   English   中英

将有序的dataFrame转换为字典,元素的开头为底部

[英]Convert ordered dataFrame into a dictionary with the elements start for the bottom

我有数据框,其中元素按“值”列排序:

ID     Value
04      1
06      2
01      3
02      4
03      5

我需要获得以点为键的字典,以点为单位的值列表以圆的顺序排列(第一个底部,第二个顶部)。

Dictionary: 
{
   01: [02,03,04,06],
   03: [04,06,01,02],
   ..
   ..
}

这是一个使用collections.deque的解决方案:

from collections import deque

dq = deque(df['ID'])
res = {}

for i in list(dq):
    res[i] = list(dq)[1:]
    dq.rotate(-1)

结果:

{'04': ['06', '01', '02', '03'],
 '06': ['01', '02', '03', '04'],
 '01': ['02', '03', '04', '06'],
 '02': ['03', '04', '06', '01'],
 '03': ['04', '06', '01', '02']}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM