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

如何在Jupyter Notebook中获取JSON并转DataFrame后仅展示前10行

如何在Jupyter Notebook中仅展示JSON数据转成DataFrame的前10行?

嘿,我来帮你搞定这个问题!你之前直接打印resp.content会输出整个原始的字节内容,自然会显示全部数据。我们只需要把JSON数据转换成pandas DataFrame,再用专门的方法限制行数就可以啦,具体步骤如下:

步骤1:确保导入必要的库

首先要确认你已经导入了requests和pandas(如果没导入的话,先运行这行):

import requests
import pandas as pd

步骤2:获取JSON数据并转换为DataFrame

把请求到的JSON数据直接转换成pandas DataFrame:

url = 'https://ckannet-storage.commondatastorage.googleapis.com/2014-12-13T15:15:31.729Z/airfields.json'
resp = requests.get(url)
# 将JSON响应转换为DataFrame
df = pd.DataFrame(resp.json())

步骤3:仅展示前10行

使用pandas的head()方法,传入参数10就能只显示前10行数据:

df.head(10)

如果你的JSON数据是嵌套结构(比如每个元素里还有子字典),可以用pd.json_normalize()来扁平化数据,再用head(10):

df = pd.json_normalize(resp.json())
df.head(10)

这样在Jupyter Notebook里运行后,就只会看到DataFrame的前10行内容啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:33:48