Reputation: 115
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?
Upvotes: 0
Views: 55
Reputation: 2202
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
Upvotes: 3