JSON文件到Pandas df [英] JSON file to Pandas df

查看:343
本文介绍了JSON文件到Pandas df的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSON文件转换为pandas df,以删除不需要的数据并将ID的csv限制为以下数据:

I'm trying to convert a JSON file into a pandas df to remove unwanted data and limit to a csv of ID's the data looks like this:

{
     "data": [
    {
      "message": "Uneeded message",
      "created_time": "2017-04-02T17:20:37+0000",
      "id": "723456782912449_1008262099345654"
    },
    {
      "message": "Uneeded message",
      "created_time": "2017-03-28T06:26:28+0000",
      "id": "771345678912449_1003934567871010"
    },

我以前没有使用过JSON,但是我用来加载此数据的代码是

I've not used JSON before but the code i've used to load this data is

import pandas as pd
import json

with open('fileName.json', encoding="utf8" ) as f:
    w = json.loads(f.read(), strict=False)

最终输出应仅为带有ID列的CSV

The end output should just be a CSV with a column of ID's

推荐答案

我认为您需要

I think you need json_normalize:

from pandas.io.json import json_normalize 
import json

with open('file.json') as data_file:    
    d = json.load(data_file)

print (d)
{
    "data": [{
        "message": "Uneeded message",
        "created_time": "2017-04-02T17:20:37+0000",
        "id": "723456782912449_1008262099345654"
    }, {
        "message": "Uneeded message",
        "created_time": "2017-03-28T06:26:28+0000",
        "id": "771345678912449_1003934567871010"
    }]
}

df = json_normalize(d, 'data')
print (df)
               created_time                                id          message
0  2017-04-02T17:20:37+0000  723456782912449_1008262099345654  Uneeded message
1  2017-03-28T06:26:28+0000  771345678912449_1003934567871010  Uneeded message

这篇关于JSON文件到Pandas df的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆