如何重塑我的数据,以便具有多个观察的ID按ID的所有可能观察对进行分组?

huangapple go评论64阅读模式
英文:

How do I reshape my data so that IDs with multiple observations are grouped as all possible pairs of observations by ID?

问题

给定一个如下的数据框:

import pandas as pd
pd.DataFrame({'id':[1,1,1,2,2], 'letter':['a','b','c','b','d'], 'value':[0,0,0,1,1]})

我想生成一个版本,其中'letter'列是所有可能的id对。顺序无关紧要((b,d) 与 (d,b) 是相同的)。这些对也不一定需要用元组表示。

pd.DataFrame({'id':[1,1,1,2], 'letter':[('a','b'),('a','c'),('b','c'),('b','d')], 'value':[0,0,0,1]})

如何将我的数据转换成所需的输出?谢谢!

英文:

Given a data frame like this:

import pandas as pd
pd.DataFrame({'id':[1,1,1,2,2], 'letter':['a','b','c','b','d'], 'value':[0,0,0,1,1]})

	   id	let	val
    0	1	a	0
    1	1	b	0
    2	1	c	0
    3	2	b	1
    4	2	d	1

I want to generate a version where the 'letter' column is all possible pairs by id. Order doesn't matter (b,d) is the same as (d,b). The pairs don't necessarily need to be represented using tuples either.

pd.DataFrame({'id':[1,1,1,2], 'letter':[('a','b'),('a','c'),('b','c'),('b','d')], 'value':[0,0,0,1]})

	id	let     val
0	1	(a, b)	0
1	1	(a, c)	0
2	1	(b, c)	0
3	2	(b, d)	1

How can I transform my data to the desired output? Thanks!

答案1

得分: 2

使用 aggregation 作为 combinationexplode

from itertools import combinations

out = (df.groupby('id', as_index=False)
         .agg({'letter': lambda x: list(combinations(x, 2)),
               'value': 'first'})
         .explode('letter')
      )

输出:

   id  letter  value
0   1  (a, b)      0
0   1  (a, c)      0
0   1  (b, c)      0
1   2  (b, d)      1
英文:

Use aggregation as a combination and explode:

from itertools import combinations

out = (df.groupby('id', as_index=False)
         .agg({'letter': lambda x: list(combinations(x, 2)),
               'value': 'first'})
         .explode('letter')
      )

Output:

   id  letter  value
0   1  (a, b)      0
0   1  (a, c)      0
0   1  (b, c)      0
1   2  (b, d)      1

huangapple
  • 本文由 发表于 2023年2月16日 05:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定