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

如何将Dataframe中字典列表列转换为结构化独立Dataframe

处理Pandas中字典列表列的转换

假设你的原始DataFrame名为df,目标列名为dict_list_col(可根据实际情况替换),可以通过以下方式将每行的字典列表转换为包含category、score、threshold三列的独立DataFrame:


单行列数据转换

如果只需要处理某一行的该列数据,比如第0行:

# 获取目标行的字典列表数据
target_dict_list = df.loc[0, 'dict_list_col']
# 直接转换为DataFrame
result_df = pd.DataFrame(target_dict_list)
# 调整列顺序(可选,确保列顺序符合需求)
result_df = result_df[['category', 'score', 'threshold']]

批量处理所有行

如果需要为每行都生成一个独立的DataFrame,可以用列表推导式批量生成:

# 生成包含每行对应DataFrame的列表
df_collection = [pd.DataFrame(row)[['category', 'score', 'threshold']] for row in df['dict_list_col']]
# 可通过索引获取对应行的结果,比如df_collection[1]就是第1行转换后的DataFrame

示例输入(字典列表)

[{'score': 0.09248554706573486, 'category': 'soccer', 'threshold': 0.13000713288784027},
 {'score': 0.09267200529575348, 'category': 'soccer', 'threshold': 0.11795613169670105},
 {'score': 0.1703065186738968, 'category': 'soccer', 'threshold': 0.2004493921995163},
 {'score': 0.08060390502214432, 'category': 'basketball', 'threshold': 0.09613725543022156},
 {'score': 0.16494056582450867, 'category': 'basketball', 'threshold': 0.2284235805273056},
 {'score': 0.008428425528109074, 'category': 'basketball', 'threshold': 0.018201233819127083},
 {'score': 0.0761604905128479, 'category': 'hockey', 'threshold': 0.0924532413482666},
 {'score': 0.10853488743305206, 'category': 'basketball', 'threshold': 0.1252049058675766},
 {'score': 0.0012563085183501244, 'category': 'soccer', 'threshold': 0.008611497469246387},
 {'score': 0.058744996786117554, 'category': 'soccer', 'threshold': 0.08366610109806061},
 {'score': 0.20794744789600372, 'category': 'rugby', 'threshold': 0.26308900117874146},
 {'score': 0.1463163197040558, 'category': 'hockey', 'threshold': 0.18053030967712402},
 {'score': 0.12938784062862396, 'category': 'hockey', 'threshold': 0.13267497718334198},
 {'score': 0.09140244871377945, 'category': 'basketball', 'threshold': 0.13820350170135498},
 {'score': 0.06976936012506485, 'category': 'hockey', 'threshold': 0.0989123210310936},
 {'score': 0.05813559517264366, 'category': 'basketball', 'threshold': 0.06885409355163574},
 {'score': 0.09365707635879517, 'category': 'hockey', 'threshold': 0.12393374741077423}]

示例输出(转换后的DataFrame)

category                  score                  threshold
0       soccer  0.09248554706573486  0.13000713288784027
1       soccer  0.09267200529575348  0.11795613169670105
2       soccer   0.1703065186738968   0.2004493921995163
3   basketball  0.08060390502214432  0.09613725543022156
4   basketball  0.16494056582450867  0.2284235805273056
5   basketball  0.008428425528109074 0.018201233819127083
6       hockey   0.0761604905128479   0.0924532413482666
7   basketball  0.10853488743305206  0.1252049058675766
8       soccer  0.0012563085183501244 0.008611497469246387
9       soccer  0.058744996786117554 0.08366610109806061
10       rugby  0.20794744789600372  0.26308900117874146
11      hockey   0.1463163197040558   0.18053030967712402
12      hockey  0.12938784062862396  0.13267497718334198
13  basketball  0.09140244871377945  0.13820350170135498
14      hockey  0.06976936012506485  0.0989123210310936
15  basketball  0.05813559517264366  0.06885409355163574
16      hockey  0.09365707635879517  0.12393374741077423

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 08:05:29