Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a dict using two columns from dataframe with duplicates in one column

I want to create a dict from 2 columns of a dataframe.

Let's say they look like this:

A         B
car1     brand1
car2     brand2
car3     brand1
car4     brand3
car5     brand2

output:

{'brand1': ['car1', 'car3'], 'brand2': ['car2', 'car5'], 'brand3': 'car4'}

There is to_dict method, however when i try to use it, i can't get it to add values to keys, instead it only maps 1 value to 1 key.

I know I can for loop column A, check value in column B with iloc and then make if else to either create a new key or add a value to existing key, but I am looking for an elegant solution.

like image 502
Alex Avatar asked Dec 15 '25 03:12

Alex


1 Answers

Borrowing from grouping rows in list in pandas groupby you can aggregate to list with a groupby, then use to_dict()

df.groupby('B')['A'].apply(list).to_dict()
{'brand1': ['car1', 'car3'], 'brand2': ['car2', 'car5'], 'brand3': ['car4']}
like image 153
G. Anderson Avatar answered Dec 16 '25 21:12

G. Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!