1

I have imported a csv file and stored its data into a dictionary with pandas as follows:

import pandas as pd
df = pd.read_csv(inputfile)
mydict = df.to_dict(orient='list')

Now, I would like to use one list to

  1. set new keys
  2. sort other lists

I have achieved something similar by doing so:

gb = df.groupby('time')
ts = dict(list(gb))

The "problem" is that I have a dictionray of dataframes instead of a dictionray of lists.

BASIC EXAMPLE

csv file:

var1 var2 var3 time var4
1    0    1.1  0    34
2    0    2.1  1    150
3    0    6    2    16
1    0    1.1  0    34
2    0    2.1  1    150
3    0    6    2    16
1    0    1.1  0    34
2    0    2.1  1    150
3    0    6    2    16

dictionary imported with pandas

mydict = 
{'var1': [1, 2, 3, 1, 2, 3, 1, 2, 3], 
'var2': [0, 0, 0, 0, 0, 0, 0, 0, 0], 
'var3': [1.1, 2.1, 6.0, 1.1, 2.1, 6.0, 1.1, 2.1, 6.0], 
'time': [0, 1, 2, 0, 1, 2, 0, 1, 2], 
'var4': [34, 150, 16, 34, 150, 16, 34, 150, 16]}

what I would like to achieve is

mydict2 = 
{0: {'var1':[1,1,1], 'var2':[0,0,0], 'var3':[1.1,1.1,1.1], 'var4':[34,34,34]},
 1: {'var1':[2,2,2], 'var2':[0,0,0], 'var3':[2.1,2.1,2.1], 'var4':[150,150,150]},
 2: {'var1':[3,3,3], 'var2':[0,0,0], 'var3':[6,6,6], 'var4':[16,16,16]}}

So far what I have obtained is similar, but for each time I get a dataframe:

ts = {0:    var1  var2  var3  time  var4
0     1     0   1.1     0    34
3     1     0   1.1     0    34
6     1     0   1.1     0    34, 
1:    var1  var2  var3  time  var4
1     2     0   2.1     1   150
4     2     0   2.1     1   150
7     2     0   2.1     1   150, 
2:    var1  var2  var3  time  var4
2     3     0   6.0     2    16
5     3     0   6.0     2    16
8     3     0   6.0     2    16}

where, if I type type(ts[0]) I get <class 'pandas.core.frame.DataFrame'> which is ok (I can get the job done), but what if I wanted a dictionart of lists?

2 Answers 2

1

You can do:

x = df.groupby("time").agg(list).to_dict("index")
print(x)

Prints:

{0: {'var1': [1, 1, 1], 'var2': [0, 0, 0], 'var3': [1.1, 1.1, 1.1], 'var4': [34, 34, 34]}, 
 1: {'var1': [2, 2, 2], 'var2': [0, 0, 0], 'var3': [2.1, 2.1, 2.1], 'var4': [150, 150, 150]}, 
 2: {'var1': [3, 3, 3], 'var2': [0, 0, 0], 'var3': [6.0, 6.0, 6.0], 'var4': [16, 16, 16]}}
Sign up to request clarification or add additional context in comments.

Comments

1

Use dict comprehension with orient='list' in DataFrame.to_dict:

d = {k: v.to_dict(orient='list') for k, v in df.set_index('time').groupby('time')}
print (d)
{0: {'var1': [1, 1, 1], 'var2': [0, 0, 0], 'var3': [1.1, 1.1, 1.1], 'var4': [34, 34, 34]}, 
 1: {'var1': [2, 2, 2], 'var2': [0, 0, 0], 'var3': [2.1, 2.1, 2.1], 'var4': [150, 150, 150]}, 
 2: {'var1': [3, 3, 3], 'var2': [0, 0, 0], 'var3': [6.0, 6.0, 6.0], 'var4': [16, 16, 16]}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.