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
- set new keys
- 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?