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

如何将特定格式JSON文件转换为结构化Pandas DataFrame?

解决方法

方法1:利用explode展开数组列

针对你已得到的包含数组列的DataFrame(df2),直接使用Pandas的explode方法,同时展开指定数组列,即可将每行数组拆分为单个数据点:

# 展开数组列并重置索引
result_df = df2.explode(['air_temperature_2m', 'time']).reset_index(drop=True)

执行后得到的result_df结构如下:

air_temperature_2mtime
0271.649999999999981646092800
1271.639999999999991646092860

方法2:直接读取并构造目标DataFrame

跳过中间的json_normalize步骤,直接解析原始JSON数据,提取对应数组后构造DataFrame,流程更简洁高效:

import pandas as pd
import json

# 读取JSON文件
with open('你的文件路径.json', 'r') as f:
    raw_data = json.load(f)

# 提取温度和时间数组
temp_list = raw_data['MIM01']['air_temperature_2m']
time_list = raw_data['time']

# 直接构造目标DataFrame
result_df = pd.DataFrame({
    'air_temperature_2m': temp_list,
    'time': time_list
})

该方法无需处理中间的数组列,直接生成符合要求的结构化数据,适合数据量较大的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 21:17:13