Pandas groupby and get dict in list

  • A+
Category:Languages

I'm trying to extract grouped row data to use values to plot it with label colors another file.

my dataframe is like below.

df = pd.DataFrame({'x': [1, 4, 5], 'y': [3, 2, 5], 'label': [1.0, 1.0, 2.0]})      x   y   label 0   1   3   1.0 1   4   2   1.0 2   5   5   2.0 

I want to get group of label list like

{'1.0': [{'index': 0, 'x': 1, 'y': 3}, {'index': 1, 'x': 4, 'y': 2}],  '2.0': [{'index': 2, 'x': 5, 'y': 5}]} 

How to do this?

 


You can use itertuples and defulatdict:

itertuples returns named tuples to iterate over dataframe:

for row in df.itertuples():     print(row) Pandas(Index=0, x=1, y=3, label=1.0) Pandas(Index=1, x=4, y=2, label=1.0) Pandas(Index=2, x=5, y=5, label=2.0) 

So taking advantage of this:

from collections import defaultdict dictionary = defaultdict(list) for row in df.itertuples():     dummy['x'] = row.x     dummy['y'] = row.y     dummy['index'] = row.Index     dictionary[row.label].append(dummy)  dict(dictionary) > {1.0: [{'x': 1, 'y': 3, 'index': 0}, {'x': 4, 'y': 2, 'index': 1}],  2.0: [{'x': 5, 'y': 5, 'index': 2}]} 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: