简体   繁体   中英

How to convert JSON data into specified Pandas DataFrame

I have a json data which looks like this:

"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

["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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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