Reputation: 719
I have a dataframe like this
ID A B
1 3 5
1 4 2
1 0 4
2 2 1
2 4 5
2 9 3
3 2 1
3 4 6
I tried code to convert them from other posts in stackoverflow
df.set_index('ID').T.to_dict('list')
But it gives me a return with each ID with only one list value
{'1': [3,5], '2': [2,1], '3': [2,1]}
Is it possible to make a dict like this?
{'1': ([3,5],[4,2],[0,4]), '2': ([2,1],[4,5],[9,3]), '3': ([2,1],[4,6])}
Dictionary keys return IDs, every ID combines with a list of tuples and every tuple contains two values.
Upvotes: 3
Views: 1620
Reputation: 294258
defaultdict
This is a good approach. It might have a for
loop, require an import
, and be multiple lines (all the things that discourage upvotes). But it is actually a good solution and very fast.
from collections import defaultdict
d = defaultdict(list)
for i, a, b in df.values.tolist():
d[i].append([a, b])
dict(d)
{1: [[3, 5], [4, 2], [0, 4]], 2: [[2, 1], [4, 5], [9, 3]], 3: [[2, 1], [4, 6]]}
Alternative
Getting a tad creative with numpy.ndarray
BTW: please don't actually do this
pd.Series(
df[['A', 'B']].values[:, None].tolist(),
df.ID.values
).sum(level=0).to_dict()
{1: [[3, 5], [4, 2], [0, 4]], 2: [[2, 1], [4, 5], [9, 3]], 3: [[2, 1], [4, 6]]}
Upvotes: 3
Reputation: 210842
In [150]: df.groupby('ID')['A','B'].apply(lambda x: x.values.tolist()).to_dict()
Out[150]:
{'1': [[3, 5], [4, 2], [0, 4]],
'2': [[2, 1], [4, 5], [9, 3]],
'3': [[2, 1], [4, 6]]}
Upvotes: 4