Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Pandas 读取 JSON


读取 JSON

大型数据集通常存储或提取为 JSON 格式。

JSON 是纯文本,但具有对象的格式,在编程世界(包括 Pandas)中广为人知。

在我们的示例中,我们将使用一个名为“data.json”的 JSON 文件。

打开 data.json.

示例

将 JSON 文件加载到 DataFrame 中

import pandas as pd

df = pd.read_json('data.json')

print(df.to_string()) 
自己试试 »

提示:使用 to_string() 打印整个 DataFrame。


字典作为 JSON

JSON = Python 字典

JSON 对象与 Python 字典具有相同的格式。

如果您的 JSON 代码不在文件中,而是在 Python 字典中,您可以将其直接加载到 DataFrame 中

示例

将 Python 字典加载到 DataFrame 中

import pandas as pd

data = {
  "Duration":{
    "0":60,
    "1":60,
    "2":60,
    "3":45,
    "4":45,
    "5":60
  },
  "Pulse":{
    "0":110,
    "1":117,
    "2":103,
    "3":109,
    "4":117,
    "5":102
  },
  "Maxpulse":{
    "0":130,
    "1":145,
    "2":135,
    "3":175,
    "4":148,
    "5":127
  },
  "Calories":{
    "0":409,
    "1":479,
    "2":340,
    "3":282,
    "4":406,
    "5":300
  }
}

df = pd.DataFrame(data)

print(df) 
自己试试 »


w3schools CERTIFIED . 2022

获得认证!

完成 Pandas 模块,完成练习,参加考试,您将获得 w3schools 认证!

10 美元报名

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.