0

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?

0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like it solved my problem, so according to this solution I have first read the file using pd.read_json after that convert it to dict, is it convert JSON data directly into dict type?
Yes! When you load data using the json library (json.load(open(filepath, 'r'))) it just gives you the data in a python dictionary

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.