Alok
Alok

Reputation: 45

Aggregated dict from pandas dataframe

My Pandas df is below. I wish to convert that to aggregated key-value pair. Below is what I have achieved and also where I am falling short.

import pandas as pd
import io

data = """
Name   factory1   factory2   factory3
Philips  China      US
Facebook  US
Taobao    China    Taiwan      Australia
"""

df = pd.read_table(io.StringIO(data), delim_whitespace=True)

df.set_index('Name').to_dict('index')

{'Philips': {'factory1': 'China', 'factory2': 'US', 'factory3': nan},
 'Facebook': {'factory1': 'US', 'factory2': nan, 'factory3': nan},
 'Taobao': {'factory1': 'China', 'factory2': 'Taiwan', 'factory3': 'Australia'}}

My expected output is :

{'Philips': {'China', 'US'},
 'Facebook': {'US'},
 'Taobao': {'China', 'Taiwan', 'Australia'}}

is there someway to aggregate!

Upvotes: 1

Views: 32

Answers (1)

BENY
BENY

Reputation: 323226

Let us try stack with groupby to_dict

out = df.set_index('Name').stack().groupby(level=0).agg(set).to_dict()
Out[109]: 
{'Facebook': {'US'},
 'Philips': {'China', 'US'},
 'Taobao': {'Australia', 'China', 'Taiwan'}}

Upvotes: 2

Related Questions