I have a json file:
{
"code":"200000",
"data":[
{
"price":"1001",
"sequence":"1636607335665",
"side":"sell",
"size":"0.00000544",
"time":1657828843154264094
},
{
"price":"5538.9",
"sequence":"1636607335668",
"side":"buy",
"size":"0.00000098",
"time":1657828843399526634
},
{
"price":"1001",
"sequence":"1636607335707",
"side":"sell",
"size":"0.00000098",
"time":1657828850316727168
},
{
"price":"1001",
"sequence":"1636607335710",
"side":"sell",
"size":"0.00000098",
"time":1657828850582673268
},
{
"price":"1001",
"sequence":"1636607335713",
"side":"sell",
"size":"0.00000098",
"time":1657828850924769384
},
{
"price":"1001",
"sequence":"1636607335716",
"side":"sell",
"size":"0.00000098",
"time":1657828850993197165
},
{
"price":"1001",
"sequence":"1636607335719",
"side":"sell",
"size":"0.00000098",
"time":1657828851193402610
},
{
"price":"5538.9",
"sequence":"1636607335722",
"side":"buy",
"size":"0.00000089",
"time":1657828857749638652
},
{
"price":"1001",
"sequence":"1636607335725",
"side":"sell",
"size":"0.00000018",
"time":1657828859913652722
},
{
"price":"1001",
"sequence":"1636607335728",
"side":"sell",
"size":"0.00000018",
"time":1657828860167682305
},
{
"price":"1001",
"sequence":"1636607335731",
"side":"sell",
"size":"0.00000018",
"time":1657828860325667720
},
{
"price":"1001",
"sequence":"1636607335734",
"side":"sell",
"size":"0.00000018",
"time":1657828860426539709
},
{
"price":"1001",
"sequence":"1636607335737",
"side":"sell",
"size":"0.00000018",
"time":1657828860605057780
},
{
"price":"5538.9",
"sequence":"1636607335740",
"side":"buy",
"size":"0.00000016",
"time":1657829347628544318
},
{
"price":"1001",
"sequence":"1636607335751",
"side":"sell",
"size":"1",
"time":1657830042459243431
},
{
"price":"1001",
"sequence":"1636607335754",
"side":"sell",
"size":"1",
"time":1657830042758958029
},
{
"price":"1001",
"sequence":"1636607335757",
"side":"sell",
"size":"1",
"time":1657830043035655903
},
{
"price":"1001",
"sequence":"1636607335760",
"side":"sell",
"size":"1",
"time":1657830043157218306
},
{
"price":"1001",
"sequence":"1636607335763",
"side":"sell",
"size":"1",
"time":1657830043346430450
},
{
"price":"5538.9",
"sequence":"1636607335766",
"side":"buy",
"size":"0.16496021",
"time":1657830098124334016
},
{
"price":"18000",
"sequence":"1636607335770",
"side":"buy",
"size":"0.22673876",
"time":1657830098399521206
},
{
"price":"18000",
"sequence":"1636607335773",
"side":"buy",
"size":"0.00000023",
"time":1657830098764936584
},
{
"price":"1001",
"sequence":"1636607335922",
"side":"sell",
"size":"0.07833984",
"time":1657830344363632786
},
{
"price":"1001",
"sequence":"1636607335925",
"side":"sell",
"size":"0.07833984",
"time":1657830344675019788
},
{
"price":"1001",
"sequence":"1636607335928",
"side":"sell",
"size":"0.07833984",
"time":1657830345026214909
},
{
"price":"1001",
"sequence":"1636607335931",
"side":"sell",
"size":"0.07833984",
"time":1657830345193678817
},
{
"price":"1001",
"sequence":"1636607335934",
"side":"sell",
"size":"0.07833984",
"time":1657830345382321251
},
{
"price":"18000",
"sequence":"1636607335937",
"side":"buy",
"size":"0.02173928",
"time":1657830368271317194
},
{
"price":"18000",
"sequence":"1636607335940",
"side":"buy",
"size":"0.00000002",
"time":1657830368537904561
},
{
"price":"1001",
"sequence":"1636607336107",
"side":"sell",
"size":"0.00434786",
"time":1657830506400266964
},
{
"price":"1001",
"sequence":"1636607336110",
"side":"sell",
"size":"0.00434786",
"time":1657830506678292287
},
{
"price":"1001",
"sequence":"1636607336113",
"side":"sell",
"size":"0.00434786",
"time":1657830506963452449
},
{
"price":"1001",
"sequence":"1636607336116",
"side":"sell",
"size":"0.00434786",
"time":1657830507262876484
},
{
"price":"1001",
"sequence":"1636607336119",
"side":"sell",
"size":"0.00434786",
"time":1657830507274604674
},
{
"price":"18000",
"sequence":"1636607336122",
"side":"buy",
"size":"0.00120653",
"time":1657830544353165335
},
{
"price":"18000",
"sequence":"1636607336125",
"side":"buy",
"size":"0.00000001",
"time":1657830544694561727
},
{
"price":"18000",
"sequence":"1636607336144",
"side":"buy",
"size":"0.75031517",
"time":1657831267172000485
},
{
"price":"19000",
"sequence":"1636607336146",
"side":"buy",
"size":"1",
"time":1657831267172000485
},
{
"price":"19349",
"sequence":"1636607336148",
"side":"buy",
"size":"0.90156477",
"time":1657831267172000485
},
{
"price":"19349",
"sequence":"1636607336153",
"side":"buy",
"size":"0.00051682",
"time":1657832244551197889
},
{
"price":"19349",
"sequence":"1636607336156",
"side":"buy",
"size":"0.00051682",
"time":1657832323552102950
},
{
"price":"19349",
"sequence":"1636607336159",
"side":"buy",
"size":"0.00051682",
"time":1657832404615114041
},
{
"price":"19349",
"sequence":"1636607336162",
"side":"buy",
"size":"0.0002",
"time":1657832453907261588
},
{
"price":"19349",
"sequence":"1636607336165",
"side":"buy",
"size":"0.00051682",
"time":1657832502167873450
},
{
"price":"19349",
"sequence":"1636607336178",
"side":"buy",
"size":"0.00516822",
"time":1657840322172772611
},
{
"price":"19349",
"sequence":"1636607336181",
"side":"buy",
"size":"0.00516822",
"time":1657841032582704012
},
{
"price":"19349",
"sequence":"1636607336188",
"side":"buy",
"size":"0.49431546",
"time":1657841056954765534
},
{
"price":"19349",
"sequence":"1636607336191",
"side":"buy",
"size":"0.00516822",
"time":1657841633410013927
},
{
"price":"19343.1953",
"sequence":"1636607336194",
"side":"sell",
"size":"0.00025848",
"time":1657841683315799309
},
{
"price":"19349",
"sequence":"1636607336202",
"side":"buy",
"size":"0.00031009",
"time":1657841883739588741
},
{
"price":"19337.3923414",
"sequence":"1636607336205",
"side":"sell",
"size":"0.00025856",
"time":1657841909289751556
},
{
"price":"19343.19529998",
"sequence":"1636607336213",
"side":"buy",
"size":"0.00025856",
"time":1657841956440581560
},
{
"price":"19349",
"sequence":"1636607336215",
"side":"buy",
"size":"0.00005161",
"time":1657841956440581560
},
{
"price":"19337.39234139",
"sequence":"1636607336220",
"side":"sell",
"size":"0.00025863",
"time":1657849200908617135
},
{
"price":"19331.59112369",
"sequence":"1636607336222",
"side":"sell",
"size":"0.00000137",
"time":1657849200908617135
},
{
"price":"19343.19529997",
"sequence":"1636607336227",
"side":"buy",
"size":"0.00025863",
"time":1657849228110938680
},
{
"price":"19349",
"sequence":"1636607336229",
"side":"buy",
"size":"0.00005154",
"time":1657849228110938680
},
{
"price":"19337.39234138",
"sequence":"1636607336234",
"side":"sell",
"size":"0.0002587",
"time":1657849268869709337
},
{
"price":"19331.59112369",
"sequence":"1636607336236",
"side":"sell",
"size":"0.0000013",
"time":1657849268869709337
},
{
"price":"19343.19529996",
"sequence":"1636607336241",
"side":"buy",
"size":"0.0002587",
"time":1657849326110758419
},
{
"price":"19349",
"sequence":"1636607336243",
"side":"buy",
"size":"0.00005147",
"time":1657849326110758419
},
{
"price":"19349",
"sequence":"1636607336248",
"side":"buy",
"size":"0.00031009",
"time":1657849345012661392
},
{
"price":"19337.39234137",
"sequence":"1636607339163",
"side":"sell",
"size":"0.00025877",
"time":1657867041820159833
},
{
"price":"19331.59112369",
"sequence":"1636607339165",
"side":"sell",
"size":"0.00025597",
"time":1657867041820159833
},
{
"price":"5689",
"sequence":"1636607339562",
"side":"buy",
"size":"0.01",
"time":1657868597265526086
},
{
"price":"1001",
"sequence":"1636607339565",
"side":"sell",
"size":"0.01",
"time":1657868598170110735
},
{
"price":"5689",
"sequence":"1636607339571",
"side":"buy",
"size":"0.00017577",
"time":1657868599016786169
},
{
"price":"1001",
"sequence":"1636607339575",
"side":"sell",
"size":"0.00017577",
"time":1657868599847374525
},
{
"price":"5689",
"sequence":"1636607340168",
"side":"buy",
"size":"0.01",
"time":1657869354756776345
},
{
"price":"1001",
"sequence":"1636607340171",
"side":"sell",
"size":"0.01",
"time":1657869355998154084
},
{
"price":"5689",
"sequence":"1636607340174",
"side":"buy",
"size":"0.00017577",
"time":1657869356825345678
},
{
"price":"1001",
"sequence":"1636607340180",
"side":"sell",
"size":"0.00017577",
"time":1657869357692624956
},
{
"price":"2500",
"sequence":"1636607341557",
"side":"buy",
"size":"0.001",
"time":1657870618541508945
},
{
"price":"2500",
"sequence":"1636607341559",
"side":"buy",
"size":"0.002",
"time":1657870618541508945
},
{
"price":"5689",
"sequence":"1636607341566",
"side":"buy",
"size":"0.00017577",
"time":1657870620769860457
},
{
"price":"1001",
"sequence":"1636607341569",
"side":"sell",
"size":"0.000999",
"time":1657870621674017603
},
{
"price":"1001",
"sequence":"1636607341647",
"side":"sell",
"size":"0.07510662",
"time":1657870777704932538
},
{
"price":"5689",
"sequence":"1636607341671",
"side":"buy",
"size":"0.00527333",
"time":1657870857711688568
},
{
"price":"5689",
"sequence":"1636607341674",
"side":"buy",
"size":"0.01",
"time":1657870868834149507
},
{
"price":"1001",
"sequence":"1636607341677",
"side":"sell",
"size":"0.01",
"time":1657870869931861093
},
{
"price":"5689",
"sequence":"1636607341680",
"side":"buy",
"size":"0.00017577",
"time":1657870870751938985
},
{
"price":"1001",
"sequence":"1636607341683",
"side":"sell",
"size":"0.000999",
"time":1657870871658420024
},
{
"price":"5689",
"sequence":"1636607341725",
"side":"buy",
"size":"0.06474014",
"time":1657870941440018617
},
{
"price":"1001",
"sequence":"1636607341743",
"side":"sell",
"size":"0.06474014",
"time":1657870960390939598
},
{
"price":"5689",
"sequence":"1636607341752",
"side":"buy",
"size":"0.002",
"time":1657870973846595055
},
{
"price":"1001",
"sequence":"1636607341767",
"side":"sell",
"size":"0.00662687",
"time":1657871009792192217
},
{
"price":"5689",
"sequence":"1636607341791",
"side":"buy",
"size":"0.004",
"time":1657871055207222592
},
{
"price":"5689",
"sequence":"1636607341878",
"side":"buy",
"size":"0.06420589",
"time":1657871246995089433
},
{
"price":"1001",
"sequence":"1636607341881",
"side":"sell",
"size":"0.06420589",
"time":1657871257132151075
},
{
"price":"5011.18714286",
"sequence":"1636607341907",
"side":"buy",
"size":"0.007",
"time":1657871357664571926
},
{
"price":"1001",
"sequence":"1636607341911",
"side":"sell",
"size":"0.007",
"time":1657871362560748791
},
{
"price":"1001",
"sequence":"1636607341968",
"side":"sell",
"size":"0.070073",
"time":1657871518530117878
},
{
"price":"1001",
"sequence":"1636607341971",
"side":"sell",
"size":"0.063382",
"time":1657871522234372733
},
{
"price":"5689",
"sequence":"1636607342013",
"side":"buy",
"size":"0.01232959",
"time":1657871675332533829
},
{
"price":"5689",
"sequence":"1636607342016",
"side":"buy",
"size":"0.01115229",
"time":1657871676052486838
},
{
"price":"5689",
"sequence":"1636607342022",
"side":"buy",
"size":"0.06362616",
"time":1657871749924575254
},
{
"price":"1001",
"sequence":"1636607342025",
"side":"sell",
"size":"0.06362616",
"time":1657871755000539664
},
{
"price":"2500",
"sequence":"1636607342521",
"side":"buy",
"size":"0.003",
"time":1657873027819080167
},
{
"price":"5689",
"sequence":"1636607342636",
"side":"buy",
"size":"0.01757778",
"time":1657873355549703658
},
{
"price":"1001",
"sequence":"1636607343484",
"side":"sell",
"size":"0.89815224",
"time":1657875048576698842
}
]}
I am trying to read out the values. Unfortunately, this does not work. What I have in mind:
I want to read first ("price" : "200") and then whether it was a sell or buy order ("side" : "sell"). The sell order should then be displayed in a line chart. The same with the buy order.
My Code:
import json
with open('events.json') as f:
json_object = json.load(f)
pairs = json_object.items()
for side, price in pairs:
print(side)
Output:
code
data
Process finished with exit code 0
My Code for the Chart:
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'X': [1, 2, 3, 4, 5],
'Y': [2, 4, 6, 10, 15]
})
# plotting a line graph
print("Line graph: ")
plt.plot(df["X"], df["Y"])
plt.show()
CodePudding user response:
You can read the dictionary data into a dataframe.
df = pd.DataFrame(data_dict)
df = df['data'].apply(pd.Series)
Filtering data
df_plot = pd.concat([df.loc[df.side.eq('buy'), ['price']].reset_index(drop=True), df.loc[df.side.eq('sell'), ['price']].reset_index(drop=True)], axis=1)
df_plot.columns = ['buy_price', 'sell_price']
Line plot
df_plot.astype('float').plot.line()
CodePudding user response:
When you iterate over pairs
you're actually iterating over keys and values, which in your case is keys = ["code", "data"]
. I think you want to loop over the data and take side
and price
of that list:
for item in pairs['data']:
price = item['price']
side = item['side']
CodePudding user response:
You can load data into dataframe from dictionary using.
import pandas as pd
df = pd.DataFrame.from_dict(data["data"])
Then, you can just use plotly library to generate the plot directly.
import plotly.express as px
px.line(df, y="price", color="side")