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

将Pandas DataFrame的JSON列转换为作者与机构列表列

Pandas提取JSON数组中的作者与机构信息

步骤说明

假设你的DataFrame已命名为df,authorships列的每个元素是解析后的JSON数组(若为字符串格式,需先转为Python对象)。

1. 预处理(若authorships为字符串格式)

如果authorships列存储的是JSON字符串而非Python列表,先执行以下转换:

import pandas as pd
import json

df['authorships'] = df['authorships'].apply(json.loads)

2. 提取所有作者名称生成authors列

将每个条目中的作者display_name提取出来,可选择以逗号分隔的字符串或列表形式存储:

字符串格式(逗号分隔)

df['authors'] = df['authorships'].apply(
    lambda x: ', '.join([entry['author']['display_name'] for entry in x])
)

列表格式

df['authors'] = df['authorships'].apply(
    lambda x: [entry['author']['display_name'] for entry in x]
)

3. 提取去重后的机构名称生成institutions列

遍历所有作者条目下的机构,提取display_name并去重,同样支持两种存储格式:

字符串格式(逗号分隔)

df['institutions'] = df['authorships'].apply(
    lambda x: ', '.join(
        list({inst['display_name'] for entry in x for inst in entry['institutions']})
    )
)

列表格式

df['institutions'] = df['authorships'].apply(
    lambda x: list({inst['display_name'] for entry in x for inst in entry['institutions']})
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 05:00:54