简体   繁体   中英

Pandas and Dictionary: How to get all unique values for each key?

I want to build a dictionary such that the value in the key-value pair is every unique value for that key.

Consider this example:

df = pd.DataFrame({'id': [1, 2, 3, 1, 2, 3], 'vals': ['a1', 'a2', 'a3', 'a2', 'a2a', 'a3a']})

# only yields last entry
dict(zip(df['id'], df['vals']))
# results
{1: 'a2', 2: 'a2a', 3: 'a3a'}

# expected value
{1: ['a1', 'a2'], 2: ['a2', 'a2a'], 3: ['a3', 'a3a']}

Use:

result = df.groupby("id")["vals"].agg(list).to_dict()
print(result)

Output

{1: ['a1', 'a2'], 2: ['a2', 'a2a'], 3: ['a3', 'a3a']}

You could use a dict comprehension, like so:

{k: group['vals'].tolist() for k, group in df.groupby('id')}

which outputs

{1: ['a1', 'a2'], 2: ['a2', 'a2a'], 3: ['a3', 'a3a']}
print(df.groupby('id')['vals'].apply(lambda x: x.tolist()).to_dict())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM