How to append more data to a dictionary - python-3.x

Assume I have this:
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
and I want to add this to the dictionary:
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
So it will look like this: (If it possible.)
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373},
{'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
I'm not good to python, so I don't know if this is possible. If it is possible how do I search for element: 'trading_pair_id': 50 and if the 'id' for that index has changed I want to update all elements with new data for that index.

I dont think the output you mentioned in your question is a valid dictionary.. It's more like a tuple.. So I came up with my code to join these two dictionaries.
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
another_tradedict = {'id': 325910, 'order_id': 109179557, 'matched_order_id': 109179004,
'direction': 'BUY', 'trading_pair_id': 50, 'symbol': 'BTC/ECS', 'amount': '0.001',
'price': '193499.99', 'date': 1654429749384}
for i in tradedict:
temp = []
temp.append(tradedict[i])
temp.append(another_tradedict[i])
tradedict[i] = temp
print(tradedict)
And the output would be something like this,
{'id': [325920, 325910], 'order_id': [109185014, 109179557], 'matched_order_id': [109181538, 109179004], 'direction': ['BUY', 'BUY'], 'trading_pair_id': [48, 50], 'symbol': ['ECS/EUR', 'BTC/ECS'], 'amount': ['1', '0.001'], 'price': ['0.1507', '193499.99'], 'date': [1654433352373, 1654429749384]}

You cannot repeat keys in a dictionary
What I meant to say is,
Your primary dictionary is having a key name "id", "order_id" etc
tradedict = {'id': 325920, 'order_id': 109185014, 'matched_order_id': 109181538,
'direction': 'BUY', 'trading_pair_id': 48, 'symbol': 'ECS/EUR', 'amount': '1',
'price': '0.1507', 'date': 1654433352373}
But the dictionary you wish to add also has the same keys, hence you will not be able to add these two dictionaries in particular.
But what you can do is:
You can add new key-value pairs in your dictionary
tradeict["Company"]="amazon"
You will get this as a result
{'id': 325920,
'order_id': 109185014,
'matched_order_id': 109181538,
'direction': 'BUY',
'trading_pair_id': 48,
'symbol': 'ECS/EUR',
'amount': '1',
'price': '0.1507',
'date': 1654433352373,
'company': 'amazon'}
Notice how new key value pair has been added to the dictionary

Related

How to add "data" to an array?

I have the below metadata in an variable, but how can I add it to an array? I would like add the "data" to an array.
{'count': 130447,
'data': [{'amount': '5',
'date': 1653943413806,
'direction': 'BUY',
'id': 314901,
'matched_order_id': 108327933,
'order_id': 108336049,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48},
{'amount': '40',
'date': 1653943403928,
'direction': 'BUY',
'id': 314900,
'matched_order_id': 108327933,
'order_id': 108336031,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48}],
'limit': 2,
'offset': 0}
Im not sure if I really get you question, please make it clearer, but from what I understood, do you want this ?
import numpy as np
dictionary = {'count': 130447,
'data': [{'amount': '5',
'date': 1653943413806,
'direction': 'BUY',
'id': 314901,
'matched_order_id': 108327933,
'order_id': 108336049,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48},
{'amount': '40',
'date': 1653943403928,
'direction': 'BUY',
'id': 314900,
'matched_order_id': 108327933,
'order_id': 108336031,
'price': '0.1326',
'symbol': 'ECS/EUR',
'trading_pair_id': 48}],
'limit': 2,
'offset': 0}
arr = np.array(dictionary['data'])
print(arr)
output:
[{'amount': '5', 'date': 1653943413806, 'direction': 'BUY', 'id': 314901, 'matched_order_id': 108327933, 'order_id': 108336049, 'price': '0.1326', 'symbol': 'ECS/EUR', 'trading_pair_id': 48}
{'amount': '40', 'date': 1653943403928, 'direction': 'BUY', 'id': 314900, 'matched_order_id': 108327933, 'order_id': 108336031, 'price': '0.1326', 'symbol': 'ECS/EUR', 'trading_pair_id': 48}]

How to collect a value from a 3 key in a dictionary?

I have the below dictionary from some API code.
I don't have much of details other the code for getting the data. But I can't figure out how it is possible to collect the 'price' if the 'direction' is BUY and 'trading_pair_id' is 48 and 'status' is 'FILLED'?
So I'm hoping that someone could help me. :)
{'count': 9,
'data': [{'amount': '0.1',
'customer_order_id': '109317257',
'date': 1654517494009,
'direction': 'BUY',
'id': 109317257,
'portfolio_id': 1,
'price': '0.1',
'remaining_amount': '0.1',
'status': 'CANCELLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [],
'trading_pair_id': 1,
'type': 'LIMIT'},
{'amount': '1',
'customer_order_id': '107613290',
'date': 1655130347003,
'direction': 'BUY',
'id': 107613290,
'portfolio_id': 1,
'price': '0.1200',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 336335,
'price': '0.12',
'time': 1655130347003,
'value': '1'}],
'trading_pair_id': 48,
'type': 'LIMIT'},
{'amount': '40',
'customer_order_id': '103699645',
'date': 1651665830607,
'direction': 'BUY',
'id': 103699645,
'portfolio_id': 1,
'price': '0.1414',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 179691,
'price': '0.141',
'time': 1651665830607,
'value': '40'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '5000',
'customer_order_id': '103655679',
'date': 1651585314315,
'direction': 'BUY',
'id': 103655679,
'portfolio_id': 1,
'price': '0.1302',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 174070,
'price': '0.1289',
'time': 1651585314315,
'value': '5000'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '1',
'customer_order_id': '103655666',
'date': 1651585258788,
'direction': 'BUY',
'id': 103655666,
'portfolio_id': 1,
'price': '0.1302',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 174068,
'price': '0.1289',
'time': 1651585258788,
'value': '1'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '0.003',
'customer_order_id': '103232017',
'date': 1651007532982,
'direction': 'SELL',
'id': 103232017,
'portfolio_id': 1,
'price': '549780.00',
'remaining_amount': '0.000',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 137448,
'price': '561000',
'time': 1651007532982,
'value': '0.003'}],
'trading_pair_id': 50,
'type': 'MARKET'}],
'limit': 10,
'offset': 0}
You can simply use list comprehension,
[entry['price'] for entry in api_response['data'] if entry['direction'] == 'BUY' and entry['status'] == 'FILLED' ...]
Or,
for entry in api_response['data']:
if entry['direction'] == 'BUY' and entry['status'] == 'FILLED' and entry['trading_pair_id'] == 48:
do_something_with(entry['price'])
if you prefer the other way
d = {'count': 9,
'data': [{'amount': '0.1',
'customer_order_id': '109317257',
'date': 1654517494009,
'direction': 'BUY',
'id': 109317257,
'portfolio_id': 1,
'price': '0.1',
'remaining_amount': '0.1',
'status': 'CANCELLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [],
'trading_pair_id': 1,
'type': 'LIMIT'},
{'amount': '1',
'customer_order_id': '107613290',
'date': 1655130347003,
'direction': 'BUY',
'id': 107613290,
'portfolio_id': 1,
'price': '0.1200',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 336335,
'price': '0.12',
'time': 1655130347003,
'value': '1'}],
'trading_pair_id': 48,
'type': 'LIMIT'},
{'amount': '40',
'customer_order_id': '103699645',
'date': 1651665830607,
'direction': 'BUY',
'id': 103699645,
'portfolio_id': 1,
'price': '0.1414',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 179691,
'price': '0.141',
'time': 1651665830607,
'value': '40'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '5000',
'customer_order_id': '103655679',
'date': 1651585314315,
'direction': 'BUY',
'id': 103655679,
'portfolio_id': 1,
'price': '0.1302',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 174070,
'price': '0.1289',
'time': 1651585314315,
'value': '5000'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '1',
'customer_order_id': '103655666',
'date': 1651585258788,
'direction': 'BUY',
'id': 103655666,
'portfolio_id': 1,
'price': '0.1302',
'remaining_amount': '0',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 174068,
'price': '0.1289',
'time': 1651585258788,
'value': '1'}],
'trading_pair_id': 48,
'type': 'MARKET'},
{'amount': '0.003',
'customer_order_id': '103232017',
'date': 1651007532982,
'direction': 'SELL',
'id': 103232017,
'portfolio_id': 1,
'price': '549780.00',
'remaining_amount': '0.000',
'status': 'FILLED',
'stop_price': None,
'time_in_force': 'GTC',
'trades': [{'id': 137448,
'price': '561000',
'time': 1651007532982,
'value': '0.003'}],
'trading_pair_id': 50,
'type': 'MARKET'}],
'limit': 10,
'offset': 0}
for i in d['data']:
if(i['direction']=='BUY' and i['trading_pair_id'] == 48 and i['status'] == 'FILLED'):
print(i['price'])

Sum Values in List of Dictionaries

I have code that is returning a list of dicts, which looks like this:
[{'id': 3605917, 'qty': 66640, 'side': 'Buy', 'time': 1609395938896, 'symbol': 'BTCUSD', 'price': 28901.5}, {'id': 3605914, 'qty': 500, 'side': 'Buy', 'time': 1609395936891, 'symbol': 'BTCUSD', 'price': 28907.5}, {'id': 3605911, 'qty': 1, 'side': 'Buy', 'time': 1609395874764, 'symbol': 'BTCUSD', 'price': 28942}, {'id': 3605449, 'qty': 70000, 'side': 'Sell', 'time': 1609384815688, 'symbol': 'BTCUSD', 'price': 28956}, {'id': 3605440, 'qty': 40, 'side': 'Sell', 'time': 1609384671382, 'symbol': 'BTCUSD', 'price': 28940}]
Reading through some previous questions, I was able to get some code to allow me to sum the total quantity for each side, where stream is the list above.
from collections import defaultdict
b = defaultdict(int)
for q in stream:
b[q['side']] += q['qty']
print(b)
This returns something that looks like this. I truncated the list above, so the numbers below won't match the example list:
defaultdict(<class 'int'>, {'Buy': 8106603, 'Sell': 1482687})
I'd like to modify the code above to show both the sum of the quantity, but also the average price. Weighted average by qty would be ideal, but would also be ok with a simple average.
For printing sum of quantities, one-liners:
a = [{'id': 3605917, 'qty': 66640, 'side': 'Buy', 'time': 1609395938896, 'symbol': 'BTCUSD', 'price': 28901.5}, {'id': 3605914, 'qty': 500, 'side': 'Buy', 'time': 1609395936891, 'symbol': 'BTCUSD', 'price': 28907.5}, {'id': 3605911, 'qty': 1, 'side': 'Buy', 'time': 1609395874764, 'symbol': 'BTCUSD', 'price': 28942}, {'id': 3605449, 'qty': 70000, 'side': 'Sell', 'time': 1609384815688, 'symbol': 'BTCUSD', 'price': 28956}, {'id': 3605440, 'qty': 40, 'side': 'Sell', 'time': 1609384671382, 'symbol': 'BTCUSD', 'price': 28940}]
print('Buy', sum(i['qty'] for i in a if i['side'] == 'Buy'))
print('Sell', sum(i['qty'] for i in a if i['side'] == 'Sell'))
For average of prices:
print(sum(i['qty']*i['price'] for i in a if i['side'] == 'Buy')/len(list(i for i in a if i['side'] == 'Buy')))
print(sum(i['qty']*i['price'] for i in a if i['side'] == 'Sell')/len(list(i for i in a if i['side'] == 'Sell')))

How to render Vega-lite viz in Google Colab

What is the recommended way of rendering a Vega-lite spec inside of Google Colab?
This post describes how to do this with Vega but not Vega-lite.
I've also tried this as suggested on Slack
chart = alt.Chart.from_dict(vl_spec_dict)
However, in Colab, I get a validation error using this valid spec converted a Python dict. The Python dict is given below.
Any suggestions would be much appreciated.
{'$schema': 'https://vega.github.io/schema/vega-lite/v4.8.1.json',
'background': 'white',
'config': {'axis': {'labelFontSize': 15,
'labelLimit': 1000,
'titleFontSize': 15},
'legend': {'labelFontSize': 15, 'symbolSize': 100, 'titleFontSize': 15},
'numberFormat': '.0f',
'scale': {'bandPaddingInner': 0.5, 'bandPaddingOuter': 0.5},
'style': {'bar': {'size': 20},
'guide-label': {'fontSize': 15},
'guide-title': {'fontSize': 15, 'value': 'asdf'}},
'title': {'fontSize': 20, 'offset': 20}},
'data': {'name': 'data-78d3ef908a3fe582b9e56995f5dff5f9'},
'datasets': {'data-78d3ef908a3fe582b9e56995f5dff5f9': [{'Year': '2011',
'counts': 1497,
'org': 'Board',
'percentage': 76},
{'Year': '2011', 'counts': 78023, 'org': 'Province', 'percentage': 65},
{'Year': '2012', 'counts': 1650, 'org': 'Board', 'percentage': 80},
{'Year': '2012', 'counts': 80429, 'org': 'Province', 'percentage': 66},
{'Year': '2013', 'counts': 1657, 'org': 'Board', 'percentage': 80},
{'Year': '2013', 'counts': 82928, 'org': 'Province', 'percentage': 68},
{'Year': '2014', 'counts': 1612, 'org': 'Board', 'percentage': 78},
{'Year': '2014', 'counts': 84985, 'org': 'Province', 'percentage': 70},
{'Year': '2015', 'counts': 1728, 'org': 'Board', 'percentage': 82},
{'Year': '2015', 'counts': None, 'org': 'Province', 'percentage': None},
{'Year': '2016', 'counts': 1844, 'org': 'Board', 'percentage': 84},
{'Year': '2016', 'counts': 85561, 'org': 'Province', 'percentage': 72},
{'Year': '2017', 'counts': 1984, 'org': 'Board', 'percentage': 85},
{'Year': '2017', 'counts': 93130, 'org': 'Province', 'percentage': 74},
{'Year': '2018', 'counts': 1950, 'org': 'Board', 'percentage': 84},
{'Year': '2018', 'counts': 93684, 'org': 'Province', 'percentage': 75}]},
'height': 300,
'layer': [{'encoding': {'color': {'field': 'org',
'title': None,
'type': 'nominal'},
'tooltip': [{'field': 'percentage',
'title': 'Percentage of Students',
'type': 'quantitative'},
{'field': 'counts',
'title': 'Number of Students',
'type': 'quantitative'},
{'field': 'org', 'title': 'level', 'type': 'nominal'}],
'x': {'axis': {'labelAngle': 55},
'field': 'Year',
'title': None,
'type': 'nominal'},
'y': {'field': 'percentage',
'scale': {'domain': [0, 100]},
'title': 'Percentage of Students',
'type': 'quantitative'}},
'mark': {'color': 'orange', 'point': True, 'type': 'line'}},
{'encoding': {'text': {'field': 'percentage', 'type': 'quantitative'},
'tooltip': [{'field': 'percentage',
'title': 'Percentage of Students',
'type': 'quantitative'},
{'field': 'counts',
'title': 'Number of Students',
'type': 'quantitative'},
{'field': 'org', 'title': 'level', 'type': 'nominal'}],
'x': {'field': 'Year', 'type': 'nominal'},
'y': {'field': 'percentage', 'type': 'quantitative'}},
'mark': {'size': 15, 'type': 'text'}}],
'title': 'Grade 3 Reading: Overall Achievement at or above the Provincial Standard',
'width': 300}
You can use the Altair HTMl renderer in Colab and then use the IPython display function to generate an out
import altair as alt
from IPython.display import display
spec = {
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"data": {
"values": [
{"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
{"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
{"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
"y": {"field": "b", "type": "quantitative"}
}
}
display(alt.display.html_renderer(spec), raw=True)

Convert a json file to a python dictionary of array

I'm a newbee in Python so excuse if my question looks, dummy.
I have a json file which look like this
[{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
and I want to read this json file in order to convert it into a dictionary of array which looks like
[{'_id': [ '1', '2','3']},
{'date': [ '2019-09-07', '2019-09-08','2019-09-09']},
{'name': [ 'abi', 'allo','ali']},
{'value': [ '0', '3','0']},
{'unit': [ '°C', '°F','°C']},]
Thank you in advance
Use collections.defaultdict
Ex:
from collections import defaultdict
data = [{'_id': '1', 'date': '2019-09-07', 'name': 'abi', 'value': 0, 'unit': '°C'},
{'_id': '2', 'date': '2019-09-08', 'name': 'allo', 'value': 3, 'unit': '°F'},
{'_id': '3', 'date': '2019-09-09', 'name': 'ali', 'value': 0, 'unit': '°C'}]
result = defaultdict(list)
for i in data:
for k, v in i.items():
result[k].append(v)
print(result)
or .setdefault
Ex:
result = {}
for i in data:
for k, v in i.items():
result.setdefault(k, []).append(v)
print(result)
Output:
{'_id': ['1', '2', '3'],
'date': ['2019-09-07', '2019-09-08', '2019-09-09'],
'name': ['abi', 'allo', 'ali'],
'unit': ['°C', '°F', '°C'],
'value': [0, 3, 0]}

Resources