英文:
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
作为 combination
和 explode
:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论