如何使用pandas按指定顺序批量提取多列中关联的科研论文数据
pandas处理Web of Science论文作者多字段关联方案
核心思路:不要拆分各字段为独立DataFrame再合并,所有解析逻辑绑定在单篇论文(单行)内完成,从根源避免重名匹配错误、顺序错位问题。
实现步骤
1. 编写单行解析函数
定义parse_wos_row()函数,输入为DataFrame的单行数据,输出为该论文下所有作者的信息列表,每个元素为单作者的属性字典:
import re import pandas as pd # 模糊匹配依赖可选,不需要可以去掉相关逻辑 from fuzzywuzzy import fuzz def parse_wos_row(row): res = [] # 1. 提取作者基准列表,作为所有字段匹配的长度基准 author_list = [a.strip() for a in row['authors'].split(';') if a.strip()] author_cnt = len(author_list) if author_cnt == 0: return res # 2. 解析邮箱地址 email_map = {} email_list = [e.strip() for e in row['email addresses'].split(';') if e.strip()] if len(email_list) == author_cnt: # 数量一致直接按顺序匹配 for auth, email in zip(author_list, email_list): email_map[auth] = email else: # 数量不一致用邮箱前缀模糊匹配 for email in email_list: prefix = email.split('@')[0].lower() best_match = max(author_list, key=lambda x: fuzz.ratio(prefix, x.lower().replace(' ', ''))) email_map[best_match] = email # 3. 解析单位地址 addr_map = {} addr_pattern = r'\[(.*?)\]\s*(.*?)(?=; \[|$)' addr_matches = re.findall(addr_pattern, row['addresses']) for auth_str, addr in addr_matches: addr_authors = [a.strip() for a in auth_str.split(';') if a.strip()] for auth in addr_authors: addr_map[auth] = addr.strip() # 4. 解析researcher_id rid_map = {} rid_list = [r.strip() for r in row['researcher_id'].split(';') if r.strip()] if len(rid_list) == author_cnt: for auth, rid in zip(author_list, rid_list): rid_map[auth] = rid else: # 可自行补充模糊匹配逻辑,匹配不到默认留空 pass # 组装单作者信息 for auth in author_list: res.append({ 'author_name': auth, 'email': email_map.get(auth, None), 'affiliation': addr_map.get(auth, None), 'researcher_id': rid_map.get(auth, None), # 按需保留原论文其他字段,比如标题、发表时间等 'paper_title': row.get('title', None) }) return res
2. 全量数据转换
直接对原始DF调用解析函数,爆炸后展开为作者维度表:
# 原始WoS导出数据读入为raw_df raw_df['author_info'] = raw_df.apply(parse_wos_row, axis=1) # 爆炸为单行单作者结构 author_df = raw_df.explode('author_info').reset_index(drop=True) # 把字典格式的作者信息展开为独立列 author_df = pd.concat([author_df, pd.json_normalize(author_df['author_info'])], axis=1) # 删除中间过渡列 author_df = author_df.drop('author_info', axis=1)
方案优势
- 单篇论文内完成所有匹配,完全避免跨论文重名、单位变动导致的匹配错误
- 无需多次表合并,流程精简3步完成,代码可复用性高
- 默认优先顺序匹配,兼容数量不一致的场景,可自行补充匹配规则提升准确率
内容的提问来源于stack exchange,提问作者Dimitri Tokmetzis
相关产品推荐
相关产品推荐

