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

如何将DataFrame关联的两个嵌套列表映射为对应键值对?

实现嵌套列表键值对映射并整合到DataFrame

需求说明

现有两个嵌套列表,分别存储潜在病症和对应的置信度:

Potential_Cond_lst = [['Any Muscle Dis'], ['Type 2 fun','My happy place','Any Endo','Any Muscle Dis'],
 ['Mad people outiside','Ox tail','Hyper T','Wu Tang'],
 ['Type 2', 'Any Endo'],
 ['Other friends', 'Encounter for friends'],
 ['Any Endo', 'Any Muscle D', 'Major Frank'],
 ['Other friends', 'Any Muscle Disease'],.....]

Confidence_lvl_lst = [['50.8%'],
 ['96.3%', '94.1%', '94.0%', '61.5%'],
 ['99.0%', '99.0%', '93.6%', '45.5%'],
 ['99.0%', '89.4%'],
 ['70.0%', '31.5%'],
 ['92.6%', '70.7%', '20.0%'],
 ['88.1%', '59.2%'], ....]

需要将两者映射为键值对形式的字符串集合,最终整合到DataFrame的指定列,输出格式如下:

ID              Reason                                                         Test Date of Reason                      Name of Test Done                            Potential Conditions with Confidence Level
0   87435   [Hanks Finger (11), Hanks left Finger (13), Hanks Right Finger (48]    2022-03-24                               [Hanks Finger (13), Hanks Left Finger (11)]  ([Any Muscle D: 50.8%])
1   49370   Franks and Beans (45)                                                  2022-07-05                               [Fransk and Beans (45)]                      ([Type 2 fun: 96.3%, My happy place:94.1% ,Any End: 94.0%, Any Muscle D: 61.5%])

实现方案

代码示例

import pandas as pd

# 原始嵌套列表数据
Potential_Cond_lst = [['Any Muscle Dis'], ['Type 2 fun','My happy place','Any Endo','Any Muscle Dis'],
 ['Mad people outiside','Ox tail','Hyper T','Wu Tang'],
 ['Type 2', 'Any Endo'],
 ['Other friends', 'Encounter for friends'],
 ['Any Endo', 'Any Muscle D', 'Major Frank'],
 ['Other friends', 'Any Muscle Disease']]

Confidence_lvl_lst = [['50.8%'],
 ['96.3%', '94.1%', '94.0%', '61.5%'],
 ['99.0%', '99.0%', '93.6%', '45.5%'],
 ['99.0%', '89.4%'],
 ['70.0%', '31.5%'],
 ['92.6%', '70.7%', '20.0%'],
 ['88.1%', '59.2%']]

# 生成目标格式的字符串列表
complete_str_lst = []
for cond_sub, conf_sub in zip(Potential_Cond_lst, Confidence_lvl_lst):
    # 配对每个病症和置信度,生成单个键值对字符串
    key_value_pairs = [f"{cond}: {conf}" for cond, conf in zip(cond_sub, conf_sub)]
    # 拼接成指定格式的字符串
    formatted_str = f"({', '.join(key_value_pairs)})"
    complete_str_lst.append(formatted_str)

# 假设已有目标DataFrame,这里示例创建一个匹配结构的DataFrame
df = pd.DataFrame({
    "ID": [87435, 49370, 12345, 67890, 54321, 98765, 11223],
    "Reason": [
        ["Hanks Finger (11)", "Hanks left Finger (13)", "Hanks Right Finger (48)"],
        "Franks and Beans (45)",
        "Noise Complaint",
        "Exercise Follow-up",
        "Social Visit",
        "Routine Check",
        "Follow-up with Friends"
    ],
    "Test Date of Reason": ["2022-03-24", "2022-07-05", "2022-10-12", "2023-01-05", "2023-03-20", "2023-06-15", "2023-09-30"],
    "Name of Test Done": [
        ["Hanks Finger (13)", "Hanks Left Finger (11)"],
        ["Fransk and Beans (45)"],
        ["Environmental Noise Test"],
        ["Fitness Assessment"],
        ["Social Interaction Survey"],
        ["General Physical Exam"],
        ["Friendship Satisfaction Quiz"]
    ]
})

# 将生成的字符串列表赋值到指定列
df["Potential Conditions with Confidence Level"] = complete_str_lst

# 输出结果
print(df)

关键逻辑说明

  1. 配对嵌套子列表:使用zip函数同时遍历两个原始嵌套列表,确保每个病症子列表与对应的置信度子列表一一匹配。
  2. 生成键值对字符串:通过列表推导式将每个病症和对应置信度拼接成"病症: 置信度"的格式。
  3. 格式化整体字符串:将每个子组的键值对用逗号连接,再包裹成(...)的格式,完全匹配目标DataFrame的列显示要求。
  4. 整合到DataFrame:直接将生成的字符串列表赋值给DataFrame的目标列,完成数据整合。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 19:45:29