Hướng dẫn read json file to dataframe python - đọc tệp json vào dataframe python

Đọc JSON

Các bộ dữ liệu lớn thường được lưu trữ hoặc trích xuất dưới dạng JSON.

JSON là văn bản đơn giản, nhưng có định dạng của một đối tượng, và được biết đến trong thế giới lập trình, bao gồm cả gấu trúc.

Trong các ví dụ của chúng tôi, chúng tôi sẽ sử dụng tệp JSON có tên là 'data.json'.

Mở dữ liệu.json.

Thí dụ

Tải tệp JSON vào DataFrame:

nhập khẩu gấu trúc dưới dạng PD

df = pd.read_json ['data.json']

print[df.to_string[]]

Hãy tự mình thử »

Mẹo: Sử dụng

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
0 để in toàn bộ khung dữ liệu. use
import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
0 to print the entire DataFrame.

Từ điển là json

JSON = Từ điển Python

Các đối tượng JSON có định dạng giống như từ điển Python.

Nếu mã JSON của bạn không có trong một tệp, nhưng trong từ điển Python, bạn có thể tải nó trực tiếp vào DataFrame:

Thí dụ

Tải tệp JSON vào DataFrame:

nhập khẩu gấu trúc dưới dạng PD

df = pd.read_json ['data.json']
  "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
  }
}

Hãy tự mình thử »

print[df]

Hãy tự mình thử »


27

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang sử dụng Python 3.6 và cố gắng tải xuống tệp JSON [350 MB] dưới dạng dữ liệu gấu trúc bằng cách sử dụng mã bên dưới. Tuy nhiên, tôi gặp lỗi sau:

data_json_str = "[" + ",".join[data] + "]
"TypeError: sequence item 0: expected str instance, bytes found

Làm cách nào để sửa lỗi?

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]

Stephen Rauch ♦

45,8K30 Huy hiệu vàng105 Huy hiệu bạc126 Huy hiệu Đồng30 gold badges105 silver badges126 bronze badges

Đã hỏi ngày 4 tháng 2 năm 2018 lúc 23:46Feb 4, 2018 at 23:46

Alberto Alvarezalberto AlvarezAlberto Alvarez

7452 Huy hiệu vàng9 Huy hiệu bạc16 Huy hiệu đồng2 gold badges9 silver badges16 bronze badges

Từ mã của bạn, có vẻ như bạn đang tải một tệp JSON có dữ liệu JSON trên mỗi dòng riêng biệt.

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
1 hỗ trợ đối số
import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
2 cho dữ liệu như sau:

data_df = pd.read_json['C:/Users/Alberto/nutrients.json', lines=True]

Lưu ý Xóa

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
3 Nếu bạn có một đối tượng JSON duy nhất thay vì các đối tượng JSON riêng lẻ trên mỗi dòng.
Remove
import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
3 if you have a single JSON object instead of individual JSON objects on each line.

Đã trả lời ngày 5 tháng 2 năm 2018 lúc 1:34Feb 5, 2018 at 1:34

0

Sử dụng mô -đun JSON, bạn có thể phân tích JSON vào đối tượng Python, sau đó tạo một khung dữ liệu từ đó:

import json
import pandas as pd
with open['C:/Users/Alberto/nutrients.json', 'r'] as f:
    data = json.load[f]
df = pd.DataFrame[data]

Đã trả lời ngày 5 tháng 2 năm 2018 lúc 0:28Feb 5, 2018 at 0:28

2

Nếu bạn mở tệp dưới dạng nhị phân [

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
4], bạn sẽ nhận được byte. Bạn nghĩ thế nào về:

with open['C:/Users/Alberto/nutrients.json', 'rU'] as f:

Cũng như đã lưu ý trong câu trả lời này, bạn cũng có thể sử dụng gấu trúc trực tiếp như:

df = pd.read_json['C:/Users/Alberto/nutrients.json', lines=True]

Đã trả lời ngày 4 tháng 2 năm 2018 lúc 23:51Feb 4, 2018 at 23:51

Stephen Rauch ♦ Stephen RauchStephen Rauch

45,8K30 Huy hiệu vàng105 Huy hiệu bạc126 Huy hiệu Đồng30 gold badges105 silver badges126 bronze badges

0

Đã hỏi ngày 4 tháng 2 năm 2018 lúc 23:46

import json
data = []
with open['nutrients.json', errors='ignore'] as f:
    for line in f:
        data.append[json.loads[line]]
print[data[0]]

Alberto Alvarezalberto AlvarezMar 13, 2019 at 2:49

7452 Huy hiệu vàng9 Huy hiệu bạc16 Huy hiệu đồng

#call the pandas library
import pandas as pd
#set the file location as URL or filepath of the json file
url = '//www.something.com/data.json'
#load the json data from the file to a pandas dataframe
df = pd.read_json[url, orient='columns']
#display the top 10 rows from the dataframe [this is to test only]
df.head[10]

Từ mã của bạn, có vẻ như bạn đang tải một tệp JSON có dữ liệu JSON trên mỗi dòng riêng biệt.

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
1 hỗ trợ đối số
import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
2 cho dữ liệu như sau:

Lưu ý Xóa

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
3 Nếu bạn có một đối tượng JSON duy nhất thay vì các đối tượng JSON riêng lẻ trên mỗi dòng.Jan 25, 2020 at 17:53

Đã trả lời ngày 5 tháng 2 năm 2018 lúc 1:34

pd.read_json["sample.json",lines=True,orient='columns']

Sử dụng mô -đun JSON, bạn có thể phân tích JSON vào đối tượng Python, sau đó tạo một khung dữ liệu từ đó:

[[{Value1:1},{value2:2}],[{value3:3},{value4:4}],.....]

Đã trả lời ngày 5 tháng 2 năm 2018 lúc 0:28

value1 = df['column_name'][0][0].get[Value1]

Nếu bạn mở tệp dưới dạng nhị phân [

import pandas as pd

# read the entire file into a python array
with open['C:/Users/Alberto/nutrients.json', 'rb'] as f:
   data = f.readlines[]

# remove the trailing "\n" from each line
data = map[lambda x: x.rstrip[], data]

# each element of 'data' is an individual JSON object.
# i want to convert it into an *array* of JSON objects
# which, in and of itself, is one large JSON object
# basically... add square brackets to the beginning
# and end, and have all the individual business JSON objects
# separated by a comma
data_json_str = "[" + ",".join[data] + "]"

# now, load it into pandas
data_df = pd.read_json[data_json_str]
4], bạn sẽ nhận được byte. Bạn nghĩ thế nào về:Jan 18, 2021 at 4:33

2

Làm thế nào để bạn tạo một khung dữ liệu từ tệp JSON trong Python?

Làm thế nào để làm nó…..
Để tạo một dữ liệu gấu trúc từ tệp JSON, trước tiên hãy nhập các thư viện Python mà bạn cần: ....
Tiếp theo, xác định một biến cho tệp JSON và nhập đường dẫn đầy đủ vào tệp: ....
Tiếp theo, hãy tạo DataFrame từ tệp JSON bằng phương thức read_json [] do gấu trúc cung cấp ..

Làm cách nào để tải JSON vào DataFrame?

Pandas Chuyển đổi chuỗi JSON thành DataFrame Bạn có thể tải Chuỗi JSON bằng hàm json.loads [].Chuyển đối tượng JSON cho json_normalize [], trả về một khung dữ liệu gấu trúc.using json. loads[] function. Pass JSON object to json_normalize[] , which returns a Pandas DataFrame.

Pandas có thể đọc dữ liệu JSON không?

Chức năng Pandas read_json [] có thể được sử dụng để đọc tệp JSON hoặc chuỗi vào DataFrame.Nó hỗ trợ JSON ở một số định dạng bằng cách sử dụng Phương Đông.JSON là tốc ký cho ký hiệu đối tượng JavaScript là định dạng tệp được sử dụng nhiều nhất được sử dụng để trao đổi dữ liệu giữa hai hệ thống hoặc ứng dụng web.. It supports JSON in several formats by using orient param. JSON is shorthand for JavaScript Object Notation which is the most used file format that is used to exchange data between two systems or web applications.

Làm cách nào để đọc các tệp JSON trong Jupyter Notebook?

Tải [] - Hàm này được sử dụng để phân tích chuỗi JSON ...
Nhập mô -đun JSON ..
Mở tệp bằng tên của tệp JSON witn open [] ..
Mở tệp bằng tên của tệp JSON witn open [] ..
Đọc tệp JSON bằng Load [] và đặt dữ liệu JSON vào một biến ..

Bài Viết Liên Quan

Chủ Đề