如何在Python中将复杂嵌套字典转换为Pandas DataFrame
问题描述
我有一个结构如下的嵌套字典:
dict = { 'series_1': ['id_series', [ ['season_1', [['ep1_title', 'ep_url'], ['ep2_title', 'ep_url']...]], ['season_2', [['ep1_title', 'ep_url'], ['ep2_title', 'ep_url']...]], ... ]], 'series_2': ['id_series', [ ['season_1', [['ep1_title', 'ep_url'], ['ep2_title', 'ep_url']...]], ['season_2', [['ep1_title', 'ep_url'], ['ep2_title', 'ep_url']...]], ... ]], ... }
示例字典:
{ 'Scooby-Doo! Mystery Incorporated': ['1660055', [ ['season 1', [['Pawn of Shadows', 'https://dl.opensubtitles.org/it/download/sub/4797725'], ['All Fear the Freak', 'https://dl.opensubtitles.org/it/download/sub/4797755']]], ['season 2', [['Through the Curtain', 'https://dl.opensubtitles.org/it/download/sub/5465599'], ['Come Undone', 'https://dl.opensubtitles.org/it/download/sub/5465681']]] ]], 'Scooby e Scrappy Doo': ['0084970', [ ['season 1', [["Scooby Roo/Scooby's Gold Medal Gambit", 'https://dl.opensubtitles.org/it/download/sub/6086643'], ['The Mark of Scooby/The Crazy Carnival Caper', 'https://dl.opensubtitles.org/it/download/sub/6086649']]] ]] }
希望将其转换为如下格式的Pandas DataFrame:
| series_title | id_series | #season | ep_title | ep_url |
|---|---|---|---|---|
| Scooby-Doo! Mystery Incorporated | 1660055 | 1 | Pawn of Shadows | https://dl.opensubtitles.org/it/download/sub/4797725 |
| Scooby-Doo! Mystery Incorporated | 1660055 | 1 | All Fear the Freak | https://dl.opensubtitles.org/it/download/sub/4797755 |
| Scooby-Doo! Mystery Incorporated | 1660055 | 2 | Through the Curtain | https://dl.opensubtitles.org/it/download/sub/5465599 |
| Scooby e Scrappy Doo | 0084970 | 1 | Scooby Roo/Scooby's Gold Medal Gambit | https://dl.opensubtitles.org/it/download/sub/6086643 |
| Scooby e Scrappy Doo | 0084970 | 1 | The Mark of Scooby/The Crazy Carnival Caper | https://dl.opensubtitles.org/it/download/sub/6086649 |
尝试过其他嵌套字典转DataFrame的方案,但不符合需求,求解决方法。
解决方案
可以通过遍历嵌套字典的层级结构,收集每一行需要的数据,最后转换为DataFrame。具体代码如下:
import pandas as pd # 示例字典 data = { 'Scooby-Doo! Mystery Incorporated': ['1660055', [ ['season 1', [['Pawn of Shadows', 'https://dl.opensubtitles.org/it/download/sub/4797725'], ['All Fear the Freak', 'https://dl.opensubtitles.org/it/download/sub/4797755']]], ['season 2', [['Through the Curtain', 'https://dl.opensubtitles.org/it/download/sub/5465599'], ['Come Undone', 'https://dl.opensubtitles.org/it/download/sub/5465681']]] ]], 'Scooby e Scrappy Doo': ['0084970', [ ['season 1', [["Scooby Roo/Scooby's Gold Medal Gambit", 'https://dl.opensubtitles.org/it/download/sub/6086643'], ['The Mark of Scooby/The Crazy Carnival Caper', 'https://dl.opensubtitles.org/it/download/sub/6086649']]] ]] } # 初始化空列表存储每行数据 rows = [] # 遍历每部剧集 for series_title, series_info in data.items(): id_series = series_info[0] seasons = series_info[1] # 遍历每季 for season in seasons: season_name = season[0] # 提取季数(比如从"season 1"中取出1) season_number = int(season_name.split()[1]) episodes = season[1] # 遍历每集 for ep in episodes: ep_title, ep_url = ep # 将数据添加到列表 rows.append({ 'series_title': series_title, 'id_series': id_series, '#season': season_number, 'ep_title': ep_title, 'ep_url': ep_url }) # 转换为DataFrame df = pd.DataFrame(rows) print(df)
代码逻辑说明:
- 层级遍历:从最外层的剧集名称开始,依次进入剧集ID、季列表、每集信息;
- 数据提取:从季名称中提取数字作为
#season字段; - 行数据收集:将每一集对应的所有字段封装为字典,存入列表;
- 转换DataFrame:用Pandas将列表直接转换为目标格式的DataFrame。
运行代码后,输出的DataFrame就会完全符合你的需求。
内容的提问来源于stack exchange,提问作者Enrico Caria
相关产品推荐
相关产品推荐

