简体   繁体   English

Pandas dataframe 到元组列表的字典

[英]Pandas dataframe to dict of list of tuples

Suppose I have the following dataframe:假设我有以下 dataframe:

df = pd.DataFrame({'id': [1,2,3,3,3], 'v1': ['a', 'a', 'c', 'c', 'd'], 'v2': ['z', 'y', 'w', 'y', 'z']})
df
id  v1  v2
1   a   z
2   a   y
3   c   w
3   c   y
3   d   z

And I want to transform it to this format:我想把它转换成这种格式:

{1: [('a', 'z')], 2: [('a', 'y')], 3: [('c', 'w'), ('c', 'y'), ('d', 'z')]}

I basically want to create a dict where the keys are the id and the values is a list of tuples of the (v1,v2) of this id.我基本上想创建一个字典,其中键是 id,值是这个 id 的 (v1,v2) 的元组列表。

I tried using groupby in id:我尝试在 id 中使用 groupby:

df.groupby('id')[['v1', 'v2']].apply(list)

But this didn't work但这没有用

Create tuples first and then pass to groupby with aggregate list :首先创建元组,然后使用聚合list传递给groupby

d = df[['v1', 'v2']].agg(tuple, 1).groupby(df['id']).apply(list).to_dict()
print (d)
{1: [('a', 'z')], 2: [('a', 'y')], 3: [('c', 'w'), ('c', 'y'), ('d', 'z')]}

Another idea is using MultiIndex :另一个想法是使用MultiIndex

d = df.set_index(['v1', 'v2']).groupby('id').apply(lambda x: x.index.tolist()).to_dict()

You can use defaultdict from the collections library:您可以使用collections库中的defaultdict

from collections import defaultdict

d = defaultdict(list)

for k, v, s in df.to_numpy():
    d[k].append((v, s))

defaultdict(list,
            {1: [('a', 'z')],
             2: [('a', 'y')],
             3: [('c', 'w'), ('c', 'y'), ('d', 'z')]})
df['New'] = [tuple(x) for x in df[['v1','v2']].to_records(index=False)]

df=df[['id','New']]
df=df.set_index('id')
df.to_dict()

Output: Output:

{'New': {1: ('a', 'z'), 2: ('a', 'y'), 3: ('d', 'z')}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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