首页 > 解决方案 > 给定在另一列中找到的约束,如何从数据框中创建对?

问题描述

我需要从电子邮件列表中随机匹配两封员工电子邮件。员工配对不能有相同的经理,也不能以前配对过。实现这一目标的最佳方法是什么?我对 Python 不是很好,所以甚至不知道如何开始。我发现的其他类似问题对我没有多大帮助。

我有两个数据集:

  1. 活跃成员列表 A 列:员工的电子邮件 B 列:员工的经理

    Emails             Managers
    jessica@xyz.com    Bob
    alex@xyz.com       Justin
    lucy@xyz.com       Justin
    eric@xyz.com       Zach
    brandon@xyz.com    Tony
    dylan@xyz.com      Patty
    
  2. 历史比赛列表

    Emails             Managers
    lucy@xyz.com       Justin
    eric@xyz.com       Zach
    

它可能是什么样子:

    Emails1            Managers1    Emails2            Managers2
    dylan@xyz.com      Patty        lucy@xyz.com       Justin
    eric@xyz.com       Zach         brandon@xyz.com    Tony
    ...

到目前为止我所拥有的(大声笑):

# Dependencies and Setup
import pandas as pd
import numpy as np
import itertools

# Load file and read in the data
active_data = pd.read_csv("Active.csv")
historical_data = pd.read_csv("Historical.csv")

# Preview data
active_data.head(7)

追溯

数据类型

标签: pythonpython-3.xloops

解决方案


试试这个,让我知道它是否有效


df['if_duplicate'] = df.duplicated(subset=['managers'])
unique_incdices = [x for x in df.shape[0] if df.loc[x,'if_duplicated']==False]

unique_incdices = [x for x in unique_incdices if x not in historical_matches['emails'].values]
ab = np.random.randint(0,len(unique_incdices),size=2)
i,j = unique_incdices[ab[0]],unique_incdices[ab[1]]

i and j are indices of two rows who 


推荐阅读