python:将JSON数据插入到数据框中

时间:2017-12-06 18:45:44

标签: python json pandas dataframe

{
"total_count": 2316913,
"incomplete_results": false,
"items": [
    {
        "id": 13737149,
        "name": "gremlins.js",
        "full_name": "marmelab/gremlins.js",
        "owner": {
            "login": "marmelab",
            "id": 3116319,
            "avatar_url": "https://avatars1.githubusercontent.com/u/3116319?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/marmelab",
            "html_url": "https://github.com/marmelab",
            "followers_url": "https://api.github.com/users/marmelab/followers",
            "following_url": "https://api.github.com/users/marmelab/following{/other_user}",
            "gists_url": "https://api.github.com/users/marmelab/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/marmelab/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/marmelab/subscriptions",
            "organizations_url": "https://api.github.com/users/marmelab/orgs",
            "repos_url": "https://api.github.com/users/marmelab/repos",
            "events_url": "https://api.github.com/users/marmelab/events{/privacy}",
            "received_events_url": "https://api.github.com/users/marmelab/received_events",
            "type": "Organization",
            "site_admin": false
        },

以上是来自Github搜索API的数据。我试图做的是拉出某些字段并将其插入数据帧。

使用标准for循环:

for i in jdata['items']:
  print(i['name']  + "\t" + i['html_url'])

打印我需要的数据,但是,它的丑陋,难以阅读,而且由于我有更多东西要提取,我处于停止点。

url = "https://api.github.com/search/repositories?q={}".format(sTerm)
jdata = requests.get(url).json()

我使用它来获取用户的输入,并将结果存储在jdata中。

数据框对我来说是新的,似乎无论我尝试什么,它都会在搜索结果中显示所有内容,或者什么也不显示。不确定我做错了什么。 (我知道我没有粘贴任何数据帧代码,没有任何工作,所以我认为我可以拯救每个人的眼癌。)

1 个答案:

答案 0 :(得分:1)

听起来你想做像

这样的事情
import pandas as pd
df = pd.DataFrame(jdata['items'], columns=['name', 'html_url'])

例如,如果sTerm'hest'

In [15]: pd.DataFrame(jdata['items'], columns=['name', 'html_url'])
Out[15]:
                 name                                        html_url
0                Hest           https://github.com/MegaCakeEater/Hest
1              heston           https://github.com/daleroberts/heston
2   NotoSansKR-Hestia  https://github.com/theeluwin/NotoSansKR-Hestia
3                hest                https://github.com/mastensg/hest
4              Heston                https://github.com/jcfrei/Heston
5                Hest           https://github.com/abirk2thebone/Hest
6                Hest                   https://github.com/h6899/Hest
7                Hest                  https://github.com/hsurce/Hest
8                hest                   https://github.com/kmajo/hest