简体   繁体   English

如何使用 python 中的 json 文件制作漂亮的 dataframe?

[英]How can I make pretty dataframe using json file in python?

I've try to save the data that I received from Open API.我尝试保存从 Open API 收到的数据。

import requests
import pandas as pd
import json
from pandas import DataFrame

def Bring_API():
    url = "api_url/authenticate key/options"
    response = requests.get(url)
    data = response.json()
    print(data)
Bring_API()

When I receive the API information from the server, it shows me the JSON file, but it's very dizzy.当我收到来自服务器的 API 信息时,它向我显示了 JSON 文件,但是很晕。 Like below.如下所示。

{"COOKRCP01":{"total_count":"1318","row":[{"RCP_PARTS_DTLS":"sweet potato soup\nsweet potato 100g, sugar 2g, rice powder 3g,water 200ml", "RCP_WAY2":"boil","MANUAL_IMG20":"","MANUAL20":"","RCP_SEQ":"17","INFO_NA":"68","INFO_WGT":"","INFO_PRO":"3","MANUAL_IMG13":"","MANUAL_IMG14":"","MANUAL_IMG15":"","MANUAL_IMG16":"","MANUAL_IMG10":"","MANUAL_IMG11":"","MANUAL_IMG12":"","MANUAL_IMG17":"","MANUAL_IMG18":"","MANUAL_IMG19":"","INFO_FAT":"6","HASH_TAG":"","MANUAL_IMG02":"http://www.foodsafetykorea.go.kr/uploadimg/cook/20_00017_2.png","MANUAL_IMG03":"http://www.foodsafetykorea.go.kr/uploadimg/cook/20_00017_3.png","RCP_PAT2":"dessert","MANUAL_IMG04":"","MANUAL_IMG05":"","MANUAL_IMG01":"http://www.foodsafetykorea.go.kr/uploadimg/cook/20_00017_1.png","MANUAL01":"1. wash and cut.","ATT_FILE_NO_MK":"http://www.foodsafetykorea.go.kr/uploadimg/cook/10_00017_1.png","MANUAL_IMG06":"","MANUAL_IMG07":"","MANUAL_IMG08":"","MANUAL_IMG09":"","MANUAL08":"","MANUAL09":"","MANUAL06":"","MANUAL07":"","MANUAL04":"4. mix.","MANUAL05":"","MANUAL02":"2. boil it.","MANUAL03":"3. make","ATT_FILE_NO_MAIN":"http://www.foodsafetykorea.go.kr/uploadimg/cook/10_00017_2.png","MANUAL11":"","MANUAL12":"","MANUAL10":"","INFO_CAR":"35","MANUAL19":"","INFO_ENG":"205","MANUAL17":"","MANUAL18":"","RCP_NM":"sweet potato soup","MANUAL15":"","MANUAL16":"","MANUAL13":"","MANUAL14":""}],"RESULT":{"MSG":"sucess.","CODE":"INFO-000"}}}

So I try to make it clear, using the pandas dataframe module.所以我试着说清楚,使用 pandas dataframe 模块。

df = pd.read_json (r'C:\Users\admin\Desktop\1.json')
print (df)

However when I doing it, It was very different from what I thought.但是,当我这样做时,它与我的想法非常不同。

                                                 COOKRCP01
RESULT               {'MSG': 'Success.', 'CODE': 'INFO-000'}
row          [{'RCP_PARTS_DTLS': 'Sweet Potato
Sweet Potato 100g, Sugar 2...
total_count                                               1318

Why can't I make the result like other pretty dataframe.为什么我不能像其他漂亮的 dataframe 那样制作结果。 ex)前任)

  0  'RCP_PARTS_DTLS'                    'RCP_NM' 
  1  Sweet potato,sugar,rice powder      SWeet Potato Soup

Can someone teach me how to eliminate the key and value in json file (or sort specific key and value from json file) what module should I use?有人可以教我如何消除 json 文件中的键和值(或从 json 文件中对特定键和值进行排序)我应该使用什么模块?

Maybe you can load it as a JSON file.也许您可以将其加载为 JSON 文件。 Using json module, json.load()使用 json 模块,json.load()

and then passing to pandas dataframe only what you want as a table:然后仅将您想要的表格传递给 pandas dataframe :

df = pd.DataFrame(data["COOKRCP01"]["row"])

LS RCP_WAY2... MANUAL13 MANUAL14 0 sweet potato soup\nsweet potato 100g, sugar 2g... boil... LS RCP_WAY2... MANUAL13 MANUAL14 0 红薯汤\n红薯100g,糖2g...煮...

[1 rows x 54 columns] [1 行 x 54 列]

You can Transpose it to get:您可以转置它以获得:

print (df.T)

RCP_PARTS_DTLS sweet potato soup\nsweet potato 100g, sugar 2g... RCP_WAY2 boil MANUAL_IMG20 RCP_PARTS_DTLS 红薯汤\n红薯 100g,糖 2g... RCP_WAY2 煮 MANUAL_IMG20
MANUAL20手册20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使我的课程在 Python 中非常可打印? - How can I make my class pretty printable in Python? 我有一个json格式的数据文件。 如何找到并打印前 20 个 eij_max 值和相关的 Pretty_formula? 我正在使用蟒蛇 - I have a data file in json format. How can I find and print the top 20 eij_max values and the associated pretty_formula? I am using python 我如何纠正我的错误并使数据帧成为使用 python 中的函数的文本文件 - How can i correct my error and make the dataframe a text file using functions in python 我如何在 python 中使用 for 循环制作 pandas dataframe 对象 - how can i make pandas dataframe objects using for loop in python 我无法使用 iPython 将我的 json 文件作为“漂亮的”JSON - I can't get my json file as "pretty" JSON using iPython 使用 Python 将 JSON 数据漂亮地打印到文件 - Pretty-Print JSON Data to a File using Python 如何以漂亮的打印风格在 JSON 中编写数据帧? - how to write dataframe in JSON in pretty print style? 如何使用Python编辑JSON文件? - How can I edit a JSON file using Python? 如何使用 python 将 xml 文件转换为 JSON? - How can i convert an xml file into JSON using python? 如何从 Python 中的 Pandas 数据帧创建嵌套的 JSON 文件? - How can I create a nested JSON file from a Pandas dataframe in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM