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.
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]]}
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]]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.