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

如何在Pandas遍历求职申请者行时拆分多组学历信息?

问题:拆分Excel中求职者的多段学历信息并处理

我用Pandas读取存储求职申请者数据的Excel表格,通过df.iterrows()遍历数据,结合Python/Selenium实现工作流程自动化。表格每行对应一位申请者,个人属性分栏存储,学历信息因一人可能有多段经历,按degree1、specialisation1、college1,degree2、specialisation2、college2…的格式分栏(最多5段学历)。我希望遍历每行时拆分出该申请者的多组学历信息并循环处理,但自定义函数未得到预期结果,寻求解决方法。

示例数据

sr_no   old_emp_id  name    address mobile  degree1 specialisation1 college1    degree2 specialisation2 college2    degree3 specialisation3 college3    emp_status
0   1   24  Amit    ABC Road    356363474   Computer Science    Robotics    IIT Delhi   MSC ML  MIT PHD AI  Harvard full-time
1   2   34  Samit   Xyz Road    367474748   Bachelor of Arts    Economics   Delih Univ  Masters of Eco  Internatioal Relation   Delhi Univ  PHD Foreign Trade   Delhi Univ  part-time
2   3   56  Richard PTC Street  363637677   Bsc Biology Mumbai Univ Masters of Science  Microbiology    Mumbai Univ PHD Communicable disease    Mumbai Univ part-time

尝试的自定义函数

def group_attributes_diff(df):
    new_data =[]
    for i in range(0, len(df),35):
        candidate_info={}
        for j in range(i,i+7+1):
            row = df.iloc[j]
            
            degree_name = row['degree_name' + str(int(j-i)//7+1)]
            specialisation = row["specialisation" + str(int(j - i)//7 + 1)] if "specialisation" + str(int(j - i)//7 + 1) in row else None
            course_start_date = row["course_start_date" + str(int(j - i)//7 + 1)] if "course_start_date" + str(int(j - i)//7 + 1) in row else None
            course_end_date = row["course_end_date" + str(int(j - i)//7 + 1)] if "course_end_date" + str(int(j - i)//7 + 1) in row else None
            marks_grades = row["marks_grades" + str(int(j - i)//7 + 1)] if "marks_grades" + str(int(j - i)//7 + 1) in row else None
            university = row["university" + str(int(j - i)//7 + 1)] if "university" + str(int(j - i)//7 + 1) in row else None
            course_type = row["course_type" + str(int(j - i)//7 + 1)] if "course_type" + str(int(j - i)//7 + 1) in row else None
            education={
             "degree_name":degree_name,
                "specialisation":specialisation,
                "course_start_date":course_start_date,
                    "course_end_date":course_end_date,
                    "marks_grades":marks_grades,
                    "university":university,
                    "course_type":course_type
                            }
        
            candidate_info["Education "+str(int(j-i)//7+1)] = education
        new_data.append(candidate_info)
    return pd.DataFrame(new_data)

test_df = group_attributes_diff(df.copy())

print(test_df.to_excel('education.xlsx'))

问题分析

你的函数存在几个核心逻辑错误:

  • 循环逻辑错误:range(0, len(df),35)是按35行一组处理数据,但实际每行对应一个申请者,完全不符合数据结构。
  • 列名不匹配:示例数据中学历字段是degree1、degree2,但代码中用了degree_name1,导致无法正确取值。
  • 遍历逻辑混乱:嵌套循环j的取值逻辑没有针对单一行提取多段学历,而是跨行了。

解决方案

正确实现思路

  1. 遍历表格的每一行(每个申请者)。
  2. 对每个申请者,循环1到5(对应最多5段学历),提取对应序号的degreeN、specialisationN、collegeN字段。
  3. 过滤掉空值的学历段(避免处理无意义的空数据)。
  4. 将提取的学历信息整理为列表,方便后续结合Selenium循环处理。

代码实现

import pandas as pd

# 读取示例数据(实际替换为你的Excel读取代码)
data = [
    [0, 1, 24, 'Amit', 'ABC Road', 356363474, 'Computer Science', 'Robotics', 'IIT Delhi', 'MSC', 'ML', 'MIT', 'PHD', 'AI', 'Harvard', 'full-time'],
    [1, 2, 34, 'Samit', 'Xyz Road', 367474748, 'Bachelor of Arts', 'Economics', 'Delih Univ', 'Masters of Eco', 'Internatioal Relation', 'Delhi Univ', 'PHD', 'Foreign Trade', 'Delhi Univ', 'part-time'],
    [2, 3, 56, 'Richard', 'PTC Street', 363637677, 'Bsc Biology', 'Mumbai Univ', 'Masters of Science', 'Microbiology', 'Mumbai Univ', 'PHD', 'Communicable disease', 'Mumbai Univ', 'part-time']
]
columns = ['sr_no', 'old_emp_id', 'name', 'address', 'mobile', 'degree1', 'specialisation1', 'college1', 'degree2', 'specialisation2', 'college2', 'degree3', 'specialisation3', 'college3', 'emp_status']
df = pd.DataFrame(data, columns=columns)

# 提取单一行的学历信息
def extract_education(row):
    education_list = []
    # 最多处理5段学历
    for n in range(1, 6):
        degree_col = f'degree{n}'
        spec_col = f'specialisation{n}'
        college_col = f'college{n}'
        
        # 跳过该段学历(如果degree为空)
        if degree_col not in row.index or pd.isna(row[degree_col]) or str(row[degree_col]).strip() == '':
            continue
        
        # 组装学历字典,空字段设为None
        education = {
            'degree': row[degree_col],
            'specialisation': row[spec_col] if (spec_col in row.index and pd.notna(row[spec_col])) else None,
            'college': row[college_col] if (college_col in row.index and pd.notna(row[college_col])) else None
        }
        education_list.append(education)
    return education_list

# 遍历每个申请者,处理数据
for idx, row in df.iterrows():
    # 提取个人基础信息
    candidate = {
        'name': row['name'],
        'mobile': row['mobile'],
        'emp_status': row['emp_status'],
        'education': extract_education(row)
    }
    
    # 这里可以加入Selenium自动化逻辑,比如填写表单
    print(f"处理申请者:{candidate['name']}")
    for idx, edu in enumerate(candidate['education'], 1):
        print(f"  第{idx}段学历:{edu['degree']} - {edu['specialisation']} @ {edu['college']}")
        # Selenium操作示例:
        # driver.find_element(By.ID, 'degree_input').send_keys(edu['degree'])
        # driver.find_element(By.ID, 'specialisation_input').send_keys(edu['specialisation'] or '')
        # ... 其他字段填写逻辑

代码说明

  • extract_education函数专门处理单一行的学历提取,循环1-5号学历段,自动跳过空值的字段。
  • 遍历df.iterrows()时,每个申请者的基础信息和学历列表被整理成字典,方便后续自动化调用。
  • 可以直接在遍历循环中嵌入Selenium代码,针对每个学历段重复执行表单填写等操作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 02:39:53