每次两个值匹配时求和

2024-04-24 18:55:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的google搜索让我失望了,我想我的主要问题是我不知道该如何表达这个问题(很抱歉这个糟糕的标题)。我试着找出每次两个人以同样的方式投票的总数。下面您将看到一个示例,说明数据的外观和我要查找的输出。我有一个工作的解决方案,但它非常缓慢(见底部),并想知道是否有一个更好的方法来处理这个问题。你知道吗

This is how the data is shaped

----------------------------------
event   person  vote
 1        a      y
 1        b      n
 1        c      nv
 1        d      nv
 1        e      y
 2        a      n
 2        b      nv
 2        c      y
 2        d      n
 2        e      n
----------------------------------

This is the output im looking for

----------------------------------
Person  a   b   c   d   e
   a    2   0   0   1   2
   b    0   2   0   0   0
   c    0   0   2   1   0
   d    1   0   1   2   1
   e    2   0   0   1   2
----------------------------------


工作代码

df = df.pivot(index='event', columns='person', values='vote')

frame = pd.DataFrame(columns=df.columns, index=df.columns)

for person1, value in frame.iterrows():

    for person2 in frame:

        count = 0 
        for i, row in df.iterrows():

            person1_votes = row[person1]
            person2_votes = row[person2]

            if person1_votes == person2_votes:
                count += 1

        frame.at[person1, person2] = count

Tags: columnstheineventdfforiscount
2条回答

@文本已经回答了你的问题。它基于寻找成对person的所有可能性的概念,并计算具有相同vote的可能性。找到所有成对的是笛卡尔积(交叉连接)。你可以从@cs95的cartesian product (CROSS JOIN) with pandas上读到一篇很棒的文章

在您的问题中,每个event计算相同的投票数,因此它是每个event的交叉连接。因此,不需要像@cs95 post那样添加helperkey列。您可以直接在event列上交叉联接。交叉连接之后,使用query过滤出那些具有相同vote的成对的person<;->;person。最后,使用crosstab成对地计算这些值。你知道吗

下面是我的解决方案:

df_match = df.merge(df, on='event').query('vote_x == vote_y')    
pd.crosstab(index=df_match.person_x, columns=df_match.person_y)

Out[1463]:
person_y  a  b  c  d  e
person_x
a         2  0  0  1  2
b         0  2  0  0  0
c         0  0  2  1  0
d         1  0  1  2  1
e         2  0  0  1  2

试着用不同的方式来看待你的问题

df=df.assign(key=1)
mergedf=df.merge(df,on=['event','key'])
mergedf['equal']=mergedf['vote_x'].eq(mergedf['vote_y'])
output=mergedf.groupby(['person_x','person_y'])['equal'].sum().unstack()
output
Out[1241]: 
person_y    a    b    c    d    e
person_x                         
a         2.0  0.0  0.0  1.0  2.0
b         0.0  2.0  0.0  0.0  0.0
c         0.0  0.0  2.0  1.0  0.0
d         1.0  0.0  1.0  2.0  1.0
e         2.0  0.0  0.0  1.0  2.0

相关问题 更多 >