You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python为Gephi生成同年龄员工关联的Excel数据

解决方案

要生成同年龄员工的双向关联(每个员工作为source对应所有其他同年龄员工作为target),可以借助itertools.permutations高效生成所有有序组合,无需手动编写循环。以下是完整实现代码:

import pandas as pd
from itertools import permutations

# 读取数据集
path = "C:\\CNT\\IBM.csv"
df = pd.read_csv(path)

# 筛选18岁员工
df_18 = df[df['Age'] == 18]

# 获取18岁员工的编号列表
employee_numbers = df_18['EmployeeNumber'].tolist()

# 生成所有有序两两组合(source ≠ target,包含双向关联)
all_associations = list(permutations(employee_numbers, 2))

# 转换为目标结构的DataFrame
result_df = pd.DataFrame(all_associations, columns=['source', 'target'])

# (可选)如果需要关联原员工的其他信息(如年龄、月薪),执行以下合并
result_df = result_df.merge(
    df_18[['EmployeeNumber', 'Age', 'MonthlyIncome']],
    left_on='source',
    right_on='EmployeeNumber',
    how='left'
).drop('EmployeeNumber', axis=1)

print(result_df)

代码说明

  • permutations(employee_numbers, 2):生成所有长度为2的有序排列,自动实现每个员工与其他所有员工的双向关联(比如297→302和302→297都会被生成),完全匹配你给出的Bob/Tom/Carl的示例结构。
  • 可选的合并步骤:如果需要保留source员工的年龄、月薪等信息,通过merge将原数据的对应字段关联到结果中。

输出示例(部分)

source  target  Age  MonthlyIncome
0     297     302   18           1420
1     297     458   18           1420
2     297     728   18           1420
3     297     829   18           1420
4     297     973   18           1420
5     297    1154   18           1420
6     297    1312   18           1420
7     302     297   18           1200
...

内容的提问来源于stack exchange,提问作者VINIJR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:45:30