1

I have a data frame as follows

         0    1    2    3    4    5    6    7    8    9
window                                                  
1000    210  227  224  223  215  239  224  217  228  218
1500    306  304  262  267  304  294  291  283  272  267
2000    294  252  362  315  286  302  358  284  318  291
2500    322  326  332  262  367  251  272  354  360  373
3000    400  253  388  400  345  400  400  359  264  310
3500    260  400  400  321  365  285  374  335  308  342
4000    400  400  400  386  304  400  332  305  350  386

I want to convert it into dictionary like

   {'1000   :[210  227  224  223  215  239  224  217  228  218] 
    '1500'  :[306  304  262  267  304  294  291  283  272  267]
    '2000'  :[294  252  362  315  286  302  358  284  318  291]
    '2500'  :[322  326  332  262  367  251  272  354  360  373]
    '3000'  :[400  253  388  400  345  400  400  359  264  310]
    '3500'  :[260  400  400  321  365  285  374  335  308  342]
    '4000'  :[400  400  400  386  304  400  332  305  350  386]}

I have tried mentioned solution by following this link

dictionary= df.set_index('window').T.to_dict('list')

print dictionary

But I am getting following error

enter image description here

Amarjit Dhillon
  • 2,718
  • 3
  • 23
  • 62

1 Answers1

2

window is already index, so need remove set_index('window'):

print (df.index)
Int64Index([1000, 1500, 2000, 2500, 3000, 3500, 4000], dtype='int64', name='window')

print (df.index.name)
window

dictionary = df.T.to_dict('list')
print (dictionary)

{2000: [294, 252, 362, 315, 286, 302, 358, 284, 318, 291], 
 3000: [400, 253, 388, 400, 345, 400, 400, 359, 264, 310], 
 4000: [400, 400, 400, 386, 304, 400, 332, 305, 350, 386], 
 2500: [322, 326, 332, 262, 367, 251, 272, 354, 360, 373], 
 1000: [210, 227, 224, 223, 215, 239, 224, 217, 228, 218], 
 3500: [260, 400, 400, 321, 365, 285, 374, 335, 308, 342], 
 1500: [306, 304, 262, 267, 304, 294, 291, 283, 272, 267]}
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252