Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a dictionary from a pandas data frame. on column contains sets

I'm trying to create a dictionary from a column containing set like

d = {'col1': [{'A','B'},{'C','D'},{'A','C'},{'C'}], 'col2': [3, 4,5,7]}
df = pd.DataFrame(data=d) 

I want to apply something such that

df.apply(something)

I get

[{A:[3,5]}, {B:[3]}, {C:[4,5,7]},{D:[4]}]
like image 826
Cristi Guevara Avatar asked Nov 20 '25 11:11

Cristi Guevara


1 Answers

Use melt, groupby + apply(list), and to_dict:

(pd.DataFrame(df.col1.tolist())
   .join(df.col2)
   .melt('col2')
   .groupby('value')['col2']
   .apply(list)
   .to_dict())
# {'A': [5, 3], 'B': [3], 'C': [7, 4, 5], 'D': [4]}
like image 60
botspeed Avatar answered Nov 23 '25 01:11

botspeed