简体   繁体   English

如何将JSON数据转换为指定的Pandas DataFrame

[英]How to convert JSON data into specified Pandas DataFrame

I have a json data which looks like this: 我有一个看起来像这样的json数据:

"rows": [
        ["2019-08-02", 364, 209, 2, 2],
    ["2019-08-03", 386, 250, 2, 5],
    ["2019-08-04", 382, 221, 3, 1],
    ["2019-08-05", 361, 218, 1, 0],
    ["2019-08-06", 338, 205, 4, 0],
    ["2019-08-07", 353, 208, 2, 2],
    ["2019-08-08", 405, 223, 2, 2],
    ["2019-08-09", 405, 266, 2, 2],
    ["2019-08-10", 494, 288, 0, 1],
        ]

I wanted to be headers of data as(not included in JSON file) as 我想成为数据的标头为(不包含在JSON文件中)为

["day", "estimatedPeopleVisited", "bought", "gives_pfeedback", "gives_nfeedback"]

I tried following code for reading file: 我尝试使用以下代码读取文件:

f = pd.read_json("data1308.json")
print(f)

and this gives output like: 这给出了如下输出:

                    rows
0   [2019-08-02, 364, 209, 2, 2]
1   [2019-08-03, 386, 250, 2, 5]
2   [2019-08-04, 382, 221, 3, 1]
3   [2019-08-05, 361, 218, 1, 0]
4   [2019-08-06, 338, 205, 4, 0]
5   [2019-08-07, 353, 208, 2, 2]
6   [2019-08-08, 405, 223, 2, 2]
7   [2019-08-09, 405, 266, 2, 2]
8   [2019-08-10, 494, 288, 0, 1]

I expect the output in form of: 我期望输出形式为:

       day      est   bought   gives_pfeedback    gives_nfeedback
0  2019-08-02   364    209           2                   2
1  2019-08-03   386    250           2                   5
2  2019-08-04   382    221           3                   1
3  2019-08-05   361    218           1                   0
4  2019-08-06   338    205           4                   0
.        .       .      .            .                   .
.        .       .      .            .                   .
.        .       .      .            .                   .

I can transform data in specified form after reading as problemset format but, is there any way to read directly JSON data in specified format? 在读取为问题集格式后,可以按指定的格式转换数据,但是,有什么方法可以直接读取指定格式的JSON数据?

What about this? 那这个呢?

import pandas as pd

data = {"rows": [
                 ["2019-08-02", 364, 209, 2, 2],
                ["2019-08-03", 386, 250, 2, 5],
                ["2019-08-04", 382, 221, 3, 1],
                ["2019-08-05", 361, 218, 1, 0],
                ["2019-08-06", 338, 205, 4, 0],
                ["2019-08-07", 353, 208, 2, 2],
                ["2019-08-08", 405, 223, 2, 2],
                ["2019-08-09", 405, 266, 2, 2],
                ["2019-08-10", 494, 288, 0, 1],
                    ]}
cols = ["day", "estimatedPeopleVisited", "bought", "gives_pfeedback", "gives_nfeedback"]

df = pd.DataFrame.from_dict(data["rows"])  
df.columns = cols

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM