python - creating a dictionary from multiple columns in a group by (pandas) -


my data frame has 'id_one' can have multiple 'id_twos' each id. each id_two has number of descriptive characteristics stored in other columns. here's example dataset.

d = {'id_one' : pd.series([123, 123, 123]),      'id_two' : pd.series([456, 567, 678]),      'descriptor' : pd.series(['blue','yellow', 'green'])}  df = pd.dataframe(d) 

i need data frame in form of 1 row per 'id_one', in 'col a' store 'id_one' , in 'col b' store values of 'id_two' dictionary keys , corresponding descriptors stored dictionary values.

any appreciated, thank you.

is you're looking for?

df.groupby('id_one').apply(lambda x: dict(zip(x['id_two'], x['descriptor']))).reset_index().rename(columns={"id_one":"col a", 0:"col b"}) #    col                                          col b # 0    123  {456: u'blue', 678: u'green', 567: u'yellow'} 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -