Check list of dic and add values - python

i have the following problem:
I have a list of dicts and want to loop through the list (temp_list) and check:
if value dic["z"] of temp[x] and temp[y] is in between the range of distance_value.
if not, insert a new dict in between temp[x] and temp[y] which contains a z_value of ( temp[y]-temp[x])/2 ), lets name it dic_x_y
afterwards fill the left over values of the new inserted dic (dic_x_y["t1"], dic_x_y["angle1"] and dic_x_y["material"]) with the values of the dic in temp[x]
Here is the data with the list and the variable:
distance_value = 1000
temp = [
{
"z": 1450,
"t1": 0,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 1950,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 12800,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 13000,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 25900,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 26000,
"t1": 10,
"angle1": 90,
"material": "Balsa 150"
}]
I searched a lot for my problem but could not find an answer.
I hope i could state my problem clearly and someone can help me.
Thanks a lot in advance.
i dont really know how to start but thats kind of my idea that i cannot get to work:
distance_value = 1000
for dic in temp:
if "dic["z"] +1 (second element of the list) - dic["z"] < distance_value:
new_dic = {"z": (dic["z"]+1 - dic["z"]), "t1": dic["t1"] , "angle1":dic["angle1"], "material":dic["material"] }
temp.insert[dic["z"]+1, new_dic]

From my json test file test.json:
[
{
"z": 1450,
"t1": 0,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 1950,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 12800,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 13000,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 25900,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 26000,
"t1": 10,
"angle1": 90,
"material": "Balsa 150"
}]
The python code:
import json
with open('test.json') as f:
temp = json.load(f)
distance_value = 1000
temp.sort(key=lambda k: k['z'])
counter = 0
Continue = True
while (Continue):
for i in range (0,len(temp)-1):
if(temp[i+1]['z'] - temp[i]['z'] > distance_value):
Continue = True
new_dic = {"z": (temp[i+1]['z'] + temp[i]['z'])/2., "t1": temp[i]['t1'], "angle1": 90, "material": temp[i]['material']}
temp.append(new_dic)
temp.sort(key=lambda k: k['z'])
break
else:
Continue = False
temp_as_string = json.dumps(temp, sort_keys=True, indent=4, separators=(',', ': '))
print(temp_as_string)
my output:
[
[
{
"z": 1450,
"t1": 0,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 1950,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 2628.125,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 3306.25,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 3984.375,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 4662.5,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 5340.625,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 6018.75,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 6696.875,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 7375.0,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 8053.125,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 8731.25,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 9409.375,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 10087.5,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 10765.625,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 11443.75,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 12121.875,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 12800,
"t1": 25,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 13000,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 13806.25,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 14612.5,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 15418.75,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 16225.0,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 17031.25,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 17837.5,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 18643.75,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 19450.0,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 20256.25,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 21062.5,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 21868.75,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 22675.0,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 23481.25,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 24287.5,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 25093.75,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 25900,
"t1": 15,
"angle1": 90,
"material": "Balsa 150"
},
{
"z": 26000,
"t1": 10,
"angle1": 90,
"material": "Balsa 150"
}
]
[Finished in 0.096s]
The logic is as follows:
Run a while loop with condition, should I keep looping over my dictionaries and check? : Continue
Inside the while loop, loop over the current list items, check if at any possible list[i+1]['z'] - list[i]['z'] is greater than set distance (this is the checking loop)
if found then make a new dict with middle point Z value, append, and re-sort (this is very important) and then break from for loop (break from checking loop at first occurrence), by breaking Continuecondition for the while loop is still true
At a stage later on in the while loop when we go through all for loop and check condition not found, then Continue is false and the while loop breaks

Related

How to split complex JSON file into multiple files by Python

I am currently splitting Json file.
The structure of JSON file is like this :
{
"id": 2131424,
"file": "video_2131424_1938263.mp4",
"metadata": {
"width": 3840,
"height": 2160,
"duration": 312.83,
"fps": 30,
"frames": 9385,
"created": "Sun Jan 17 17:48:52 2021"
},
"frames": [
{
"number": 207,
"image": "frame_207.jpg",
"annotations": [
{
"label": {
"x": 730,
"y": 130,
"width": 62,
"height": 152
},
"category": {
"code": "child",
"attributes": [
{
"code": "global_id",
"value": "7148"
}
]
}
},
{
"label": {
"x": 815,
"y": 81,
"width": 106,
"height": 197
},
"category": {
"code": "person",
"attributes": []
}
}
]
},
{
"number": 221,
"image": "frame_221.jpg",
"annotations": [
{
"label": {
"x": 730,
"y": 130,
"width": 64,
"height": 160
},
"category": {
"code": "child",
"attributes": [
{
"code": "global_id",
"value": "7148"
}
]
}
},
{
"label": {
"x": 819,
"y": 82,
"width": 106,
"height": 200
},
"category": {
"code": "person",
"attributes": []
}
}
]
},
{
"number": 236,
"image": "frame_236.jpg",
"annotations": [
{
"label": {
"x": 731,
"y": 135,
"width": 74,
"height": 160
},
"category": {
"code": "child",
"attributes": [
{
"code": "global_id",
"value": "7148"
}
]
}
},
{
"label": {
"x": 821,
"y": 83,
"width": 106,
"height": 206
},
"category": {
"code": "person",
"attributes": []
}
}
]
},
I have to extract [x, y, width, height] from each label.
I tried some code like this:
file = json.load(open('annotation_2131424.json'))
file['frames'][i]['annotations'][j]['label']['x']
But I cannot split JSON.
I tried like this but I cannot run...
I hope I've understood your question right. To get x, y, width, height from each label (dct is your dictionary from the question):
out = [
[
[
a["label"]["x"],
a["label"]["y"],
a["label"]["width"],
a["label"]["height"],
]
for a in frame["annotations"]
]
for frame in dct["frames"]
]
print(out)
Prints:
[
[[730, 130, 62, 152], [815, 81, 106, 197]],
[[730, 130, 64, 160], [819, 82, 106, 200]],
[[731, 135, 74, 160], [821, 83, 106, 206]],
]

fetching multiple vales and keys from dict

movies={
'actors':{'prabhas':{'knownAs':'Darling', 'awards':{'nandi':1, 'cinemaa':1, 'siima':1},'remuneration':100, 'hits':{'industry':2, 'super':3,'flops':8}, 'age':41, 'height':6.1, 'mStatus':'single','sRate':'35%'},
'pavan':{'knownAs':'Power Star', 'awards':{'nandi':2, 'cinemaa':2, 'siima':5}, 'hits':{'industry':2, 'super':7,'flops':16}, 'age':48, 'height':5.9, 'mStatus':'married','sRate':'37%','remuneration':50},
},
'actress':{
'tamanna':{'knownAs':'Milky Beauty', 'awards':{'nandi':0, 'cinemaa':1, 'siima':1}, 'remuneration':10, 'hits':{'industry':1, 'super':7,'flops':11}, 'age':28, 'height':5.9, 'mStatus':'single', 'sRate':'40%'},
'rashmika':{'knownAs':'Butter Milky Beauty', 'awards':{'nandi':0, 'cinemaa':0, 'siima':2}, 'remuneration':12,'hits':{'industry':0, 'super':4,'flops':2}, 'age':36, 'height':5.9, 'mStatus':'single', 'sRate':'30%'},
1.What are the total number of Nandi Awards won by actors?
2. What is the success rate of Prince?
3.What is the name of Prince?
you can answer the first question with this:
import jmespath
movies={
"actors": {
"prabhas": {
"knownAs": "Darling",
"awards": {
"nandi": 1,
"cinemaa": 1,
"siima": 1
},
"remuneration": 100,
"hits": {
"industry": 2,
"super": 3,
"flops": 8
},
"age": 41,
"height": 6.1,
"mStatus": "single",
"sRate": "35%"
},
"pavan": {
"knownAs": "Power Star",
"awards": {
"nandi": 2,
"cinemaa": 2,
"siima": 5
},
"hits": {
"industry": 2,
"super": 7,
"flops": 16
},
"age": 48,
"height": 5.9,
"mStatus": "married",
"sRate": "37%",
"remuneration": 50
}
},
"actress": {
"tamanna": {
"knownAs": "Milky Beauty",
"awards": {
"nandi": 0,
"cinemaa": 1,
"siima": 1
},
"remuneration": 10,
"hits": {
"industry": 1,
"super": 7,
"flops": 11
},
"age": 28,
"height": 5.9,
"mStatus": "single",
"sRate": "40%"
},
"rashmika": {
"knownAs": "Butter Milky Beauty",
"awards": {
"nandi": 0,
"cinemaa": 0,
"siima": 2
},
"remuneration": 12,
"hits": {
"industry": 0,
"super": 4,
"flops": 2
},
"age": 36,
"height": 5.9,
"mStatus": "single",
"sRate": "30%"
}
}
}
total_nandies_by_actors = sum(jmespath.search('[]',jmespath.search('actors.*.*.nandi',movies)))
but there is no Prince in the data you've provided

merge dicts that have the same value for specific key

I need to combine dictionaries that have the same value for the key "tag".
Like from this:
[
[
{
"tag": "#2C00L02RU",
"stamina": 233
},
{
"tag": "#8YG8RJV90",
"stamina": 20
},
{
"tag": "#LQV2JCPR",
"stamina": 154
},
{
"tag": "#9JQLPGLJJ",
"stamina": 134
}
],
[
{
"tag": "#2C00L02RU",
"health": 200
},
{
"tag": "#8YG8RJV90",
"health": 40
},
{
"tag": "#LQV2JCPR",
"health": 100
},
{
"tag": "#9JQLPGLJJ",
"health": 240
}
],
[
{
"tag": "#LQV2JCPR",
"fame": 1
},
{
"tag": "#8YG8RJV90",
"fame": 2
},
{
"tag": "#9JQLPGLJJ",
"fame": 3
},
{
"tag": "#2C00L02RU",
"fame": 4
}
],
[
{
"tag": "#LQV2JCPR",
"moves": 6
},
{
"tag": "#8YG8RJV90",
"moves": 0
},
{
"tag": "#9JQLPGLJJ",
"moves": 8
},
{
"tag": "#2C00L02RU",
"moves": 4
}
]
]
to this:
[
{
"tag": "#2C00L02RU",
"stamina": 233,
"health": 200,
"fame": 4,
"moves": 4
},
{
"tag": "#8YG8RJV90",
"stamina": 20,
"health": 40,
"fame": 2,
"moves": 2
},
{
"tag": "#LQV2JCPR",
"stamina": 154,
"health": 100,
"fame": 1,
"moves": 6
},
{
"tag": "#9JQLPGLJJ",
"stamina": 134,
"health": 240,
"fame": 3,
"moves": 8
}
]
I've already tried iterating through countless loops, but only got failures.
I won't show any of my attempts here because they didn't even come close to the expected result.
If you need any other information, just let me know.
If lst is list from your question, you can do:
out = {}
for l in lst:
for d in l:
out.setdefault(d["tag"], {}).update(d)
print(list(out.values()))
Prints:
[
{"tag": "#2C00L02RU", "stamina": 233, "health": 200, "fame": 4, "moves": 4},
{"tag": "#8YG8RJV90", "stamina": 20, "health": 40, "fame": 2, "moves": 0},
{"tag": "#LQV2JCPR", "stamina": 154, "health": 100, "fame": 1, "moves": 6},
{"tag": "#9JQLPGLJJ", "stamina": 134, "health": 240, "fame": 3, "moves": 8},
]

Why is the individual dictionary being printed in output in the following code not the same as the one being added?

So, I have a JSON file, with some elements in it. The end goal of the code is to generate 3 elements for each element in the JSON file, with some modified properties, but not all.
The way I went about this is to run a for loop and have 3 different parts of code block generating the required elements, and adding it to an empty list. But for some reason, I the elements I am generating aren't the ones being added to the final list.
Specifically, the "label" sub element gets messed up. Can someone please explain why's that happening?
Here's the code:
from math import sqrt
import json
import gc
with open("your-path here\\inclined-conveyors.json") as f:
conv_json = json.load(f)
#print("conv_json['conv'] object is: \n\n\n", conv_json['conv'])
modified_conv = list()
for item in conv_json["conv"]:
new_x_coordinate_delta = (item['height']) / (2*sqrt(3))
new_y_coordinate_delta = item['height'] / 6
addendum1 = item.copy()
addendum1['name'] = addendum1['name'][:6] + "A"
addendum1['displayName'] = addendum1['name']
new_x_coordinate = addendum1['x'] + new_x_coordinate_delta
new_y_coordinate = addendum1['y'] - new_y_coordinate_delta
addendum1['x'] = round(new_x_coordinate, 6)
addendum1['y'] = round(new_y_coordinate, 6)
addendum1['label']['x'] = round(new_x_coordinate, 6)
addendum1['label']['y'] = round(new_y_coordinate, 6)
addendum1['height'] = addendum1['height'] / 3
print("\n addendum1 is: ", addendum1)
empty_list = []
empty_list.append(addendum1)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
del addendum1
del empty_list
gc.collect()
# print("added item from x == 1")
# print("\n modified conv is: ", modified_conv)
addendum2 = item.copy()
addendum2['name'] = addendum2['name'][:6] + "B"
addendum2['displayName'] = addendum2['name']
addendum2['label']['x'] = addendum2['x']
addendum2['label']['y'] = addendum2['y']
addendum2['height'] = addendum2['height'] / 3
print("\n addendum2 is: ", addendum2)
empty_list = []
empty_list.append(addendum2)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
# modified_conv = modified_conv + addendum
del addendum2
del empty_list
gc.collect()
# print("added item from x == 2")
# print("\n modified conv is: ", modified_conv)
addendum3 = item.copy()
addendum3['name'] = addendum3['name'][:6] + "C"
addendum3['displayName'] = addendum3['name']
#addendum3['displayName'] = ""
new_x_coordinate = addendum3['x'] - new_x_coordinate_delta
new_y_coordinate = addendum3['y'] + new_y_coordinate_delta
addendum3['x'] = round(new_x_coordinate, 6)
addendum3['y'] = round(new_y_coordinate, 6)
addendum3['label']['x'] = round(new_x_coordinate, 6)
addendum3['label']['y'] = round(new_y_coordinate, 6)
addendum3['height'] = addendum3['height'] / 3
print("\n addendum3 is: ", addendum3)
empty_list = []
empty_list.append(addendum3)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
# modified_conv = modified_conv + addendum
del addendum3
del empty_list
gc.collect()
# print("added item from x == 3")
# print("\n modified conv is: ", modified_conv)
# modified_conv["conv"].append(item)
print("\n\n\n modified conv is: \n\n\n", modified_conv)
print("\n\n\ number of items in json is: ", len(conv_json['conv']))
print("\n\n number of items in modified conv is: ", len(modified_conv))
Here is a sample JSON:
{
"conv": [
{
"className": "ConveyorStraight",
"name": "P42700B",
"displayName": "P42700",
"type": "Straight",
"x": 1511,
"y": 2891.5,
"label": {
"x": 1511,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42600B",
"displayName": "P42600",
"type": "Straight",
"x": 1621,
"y": 2891.5,
"label": {
"x": 1621,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42500B",
"displayName": "P42500",
"type": "Straight",
"x": 1731,
"y": 2891.5,
"label": {
"x": 1731,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42400B",
"displayName": "P42400",
"type": "Straight",
"x": 1861,
"y": 2892.5,
"label": {
"x": 1861,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42300B",
"displayName": "P42300",
"type": "Straight",
"x": 1971,
"y": 2892.5,
"label": {
"x": 1971,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42200B",
"displayName": "P42200",
"type": "Straight",
"x": 2081,
"y": 2892.5,
"label": {
"x": 2081,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42100B",
"displayName": "P42100",
"type": "Straight",
"x": 2211,
"y": 2892.5,
"label": {
"x": 2211,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42000B",
"displayName": "P42000",
"type": "Straight",
"x": 2321,
"y": 2892.5,
"label": {
"x": 2321,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41900B",
"displayName": "P41900",
"type": "Straight",
"x": 2431,
"y": 2892.5,
"label": {
"x": 2431,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41800B",
"displayName": "P41800",
"type": "Straight",
"x": 2561,
"y": 2892.5,
"label": {
"x": 2561,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41700B",
"displayName": "P41700",
"type": "Straight",
"x": 2671,
"y": 2892.5,
"label": {
"x": 2671,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41600B",
"displayName": "P41600",
"type": "Straight",
"x": 2781,
"y": 2892.5,
"label": {
"x": 2781,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41500B",
"displayName": "P41500",
"type": "Straight",
"x": 2911,
"y": 2892.5,
"label": {
"x": 2911,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41400B",
"displayName": "P41400",
"type": "Straight",
"x": 3021,
"y": 2892.5,
"label": {
"x": 3021,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41300B",
"displayName": "P41300",
"type": "Straight",
"x": 3131,
"y": 2892.5,
"label": {
"x": 3131,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41200B",
"displayName": "P41200",
"type": "Straight",
"x": 3261,
"y": 2892.5,
"label": {
"x": 3261,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41100B",
"displayName": "P41100",
"type": "Straight",
"x": 3371,
"y": 2892.5,
"label": {
"x": 3371,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
}
]
}

Grouping data by date in MongoDB and Python

I'm making a standard find query to my MongoDB database, it looks like this:
MyData = pd.DataFrame(list(db.MyData.find({'datetimer': {'$gte': StartTime, '$lt': Endtime}})), columns=['price', 'amount', 'datetime'])
Now i'm trying to do another query, but it's more complicated and i don't know how to do it. Here is a sample of my data:
{"datetime": "2020-07-08 15:10", "price": 21, "amount": 90}
{"datetime": "2020-07-08 15:15", "price": 22, "amount": 50}
{"datetime": "2020-07-08 15:19", "price": 21, "amount": 40}
{"datetime": "2020-07-08 15:30", "price": 21, "amount": 90}
{"datetime": "2020-07-08 15:35", "price": 32, "amount": 50}
{"datetime": "2020-07-08 15:39", "price": 41, "amount": 40}
{"datetime": "2020-07-08 15:49", "price": 32, "amount": 40}
I need to group that data in intervals of 30 Minutes and have them distinct by price. So all the records before 15:30must have 15:30 as datetime, all the records before 16:00 need to have 16:00. An example of the expected output:
The previous data becomes this:
{"datetime": "2020-07-08 15:30", "price": 21, "amount": 90}
{"datetime": "2020-07-08 15:30", "price": 22, "amount": 50}
{"datetime": "2020-07-08 16:00", "price": 32, "amount": 50}
{"datetime": "2020-07-08 16:00", "price": 41, "amount": 40}
I don't know if this query is doable, so any kind of advice is appreciated. I can also do that from my code, if it's not possible to do
I tried the code suggested here, but i got the following result, which is not the expected output:
Query = db.myData.aggregate([
{ "$group": {
"_id": {
"$toDate": {
"$subtract": [
{ "$toLong": "$datetime" },
{ "$mod": [ { "$toLong": "$datetime" }, 1000 * 60 * 15 ] }
]
}
},
"count": { "$sum": 1 }
}}
])
for x in Query:
print(x)
//OUTPUT:
{'_id': datetime.datetime(2020, 7, 7, 9, 15), 'count': 39}
{'_id': datetime.datetime(2020, 7, 6, 18, 30), 'count': 44}
{'_id': datetime.datetime(2020, 7, 7, 16, 30), 'count': 54}
{'_id': datetime.datetime(2020, 7, 7, 11, 45), 'count': 25}
{'_id': datetime.datetime(2020, 7, 6, 22, 15), 'count': 48}
{'_id': datetime.datetime(2020, 7, 7, 15, 0), 'count': 30}
...
What #Gibbs suggested is correct, you just have to modify the data a little bit.
Check if the below aggregate query is what you are looking for
Query = db.myData.aggregate([
{
"$group": {
"_id": {
"datetime":{
"$toDate": {
"$subtract": [
{ "$toLong": "$datetime" },
{ "$mod": [ { "$toLong": "$datetime" }, 1000 * 60 * 30 ] }
]
}
},
"price": "$price",
"amount": "$amount"
},
}
},
{
"$replaceRoot": { "newRoot": "$_id"}
}
])
for x in Query:
print(x)

Categories