[英]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.