python - 给定在另一列中找到的约束,如何从数据框中创建对?
问题描述
我需要从电子邮件列表中随机匹配两封员工电子邮件。员工配对不能有相同的经理,也不能以前配对过。实现这一目标的最佳方法是什么?我对 Python 不是很好,所以甚至不知道如何开始。我发现的其他类似问题对我没有多大帮助。
我有两个数据集:
活跃成员列表 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
历史比赛列表
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)
解决方案
试试这个,让我知道它是否有效
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
推荐阅读
- c# - SQL 可能会导致循环或多个级联路径
- python - SQLAlchemy:在存储之前映射列值
- emscripten - 在 Emscripten 上使用 ofSoundStream 听不到声音
- spring-webflux - 检索 StepVerifier 中的所有通量元素
- xamarin - 发布模式下的 MvvmCross Android 绑定 EditText
- hadoop - 配置单元控制台中的所有配置单元命令都给出异常
- c - 有没有一种按位数使用二叉树的排序算法?
- python - 识别数据框行内的 HTML 标签
- azure - 使用 Azure Logic App 和 Azure Function 更改数据库时使 redis 缓存失效
- javascript - ActionController::RoutingError (没有路由匹配 [GET] )