Most Common (Max) Items in JSON data - python
How can I split this JSON data into groups according to 'name' and sum the number of 'items' in each group in order to find the most common name (based on number of items). The JSON data I am working with is as follows:
json_data= [
{'code': '0101010G0AAABAB',
'items': 2,
'practice': 'N81013',
'name': 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F',
'nic': 5.98,
'act_cost': 5.56,
'quantity': 1000},
{'code': '0101021B0AAAHAH',
'items': 1,
'practice': 'N81013',
'name': 'Alginate_Raft-Forming Oral Susp S/F',
'nic': 1.95,
'act_cost': 1.82,
'quantity': 500},
{'code': '0101021B0AAALAL',
'items': 12,
'practice': 'N81013',
'name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 64.51,
'act_cost': 59.95,
'quantity': 6300},
{'code': '0101021B0AAAPAP',
'items': 3,
'practice': 'N81013',
'name': 'Sod Alginate/Pot Bicarb_Tab Chble 500mg',
'nic': 9.21,
'act_cost': 8.55,
'quantity': 180},
{'code': '0101021B0BEADAJ',
'items': 6,
'practice': 'N81013',
'name': 'Gaviscon Advance_Liq (Peppermint) S/F',
'nic': 28.92,
'act_cost': 26.84,
'quantity': 90},
{'code': '0101021B0BEAIAL',
'items': 15,
'practice': 'N81013',
'name': 'Gaviscon Advance_Liq (Peppermint) S/F',
'nic': 82.62,
'act_cost': 76.67,
'quantity': 7800},
{'code': '0101021B0BEAQAP',
'items': 5,
'practice': 'N81013',
'name': 'Gaviscon Advance_Liq (Peppermint) S/F',
'nic': 13.47,
'act_cost': 12.93,
'quantity': 116},
{'code': '0101021B0BEBEAL',
'items': 10,
'practice': 'N81013',
'name': 'Gaviscon Advance_Liq (Peppermint) S/F',
'nic': 64.0,
'act_cost': 59.45,
'quantity': 6250},
{'code': '0101021B0BIABAH',
'items': 2,
'practice': 'N81013',
'name': 'Sod Algin/Pot Bicarb_Susp S/F',
'nic': 3.9,
'act_cost': 3.64,
'quantity': 1000},
{'code': '0102000A0AAAAAA',
'items': 1,
'practice': 'N81013',
'name': 'Alverine Cit_Cap 60mg',
'nic': 19.48,
'act_cost': 18.05,
'quantity': 100}]
I have been able to identify the number of unique values for 'name' but I do not know how to proceed from there. Here is the code I used:
names =[]
for item in range(len(json_data)):
names.append(json_data[item]['name'])
names=set(names)
names=list(names)
print(len(names))
I expect the output to be in the following format:
most_common = [("", 0)]
with the name in quotes followed by the total sum of items.
e.g:
most_common = [("Gaviscon Advance_Liq (Peppermint) S/F", 36)]
Please bear with me. I am new to Stackoverflow and this is my first question so I'm still trying to get used to how to ask a question here.
You can use a Counter. It is a class from collections module that allows you to easily count how many times each item appears in a collection.
>>> from collections import Counter
>>> name_numbers = Counter()
>>> for item in json_data:
... name_numbers[item['name']] += item['items']
...
>>> name_numbers
Counter({'Gaviscon Advance_Liq (Peppermint) S/F': 36, 'Sod Algin/Pot Bicarb_Susp S/F': 14, 'Sod Alginate/Pot Bicarb_Tab Chble 500mg': 3, 'Co-Magaldrox_Susp 195mg/220mg/5ml S/F': 2, 'Alginate_Raft-Forming Oral Susp S/F': 1, 'Alverine Cit_Cap 60mg': 1})
>>> name_numbers.most_common(1)
[('Gaviscon Advance_Liq (Peppermint) S/F', 36)]
Related
python sort list by custom criteria
I have following data read from csv : venues =[{'capacity': 700, 'id': 1, 'name': 'AMD'}, {'capacity': 2000, 'id': 2, 'name': 'Honda'}, {'capacity': 2300, 'id': 3, 'name': 'Austin Kiddie Limits'}, {'capacity': 2000, 'id': 4, 'name': 'Austin Ventures'}] i get the unique keys with : b= list({k for d in venues for k in d.keys()}) which results in random order : ['name', 'capacity', 'id'] i would like to sort the unique key result in following manner : sorted_keys = ['id','name','capacity'] how may i achieve this ?
In python tuples are sorted element-wise, so using a key function that produces tuple from your dictionaries should do the trick. >>> sorted(venues, key=lambda row: (row['id'], row['name'], row['capacity'])) To be slightly more concise, you could use operator.itemgetter. >>> from operator import itemgetter >>> sorted(venues, key=itemgetter('id','name','capacity'))
You can use sort() function and its property key to introduce specific criteria when sorting your list: venues =[{'capacity': 700, 'id': 1, 'name': 'AMD'}, {'capacity': 2000, 'id': 2, 'name': 'Honda'}, {'capacity': 2300, 'id': 3, 'name': 'Austin Kiddie Limits'}, {'capacity': 2000, 'id': 4, 'name': 'Austin Ventures'}] venues.sort(key=lambda x: x["capacity"]) print(venues) Output: In this case it sorts by capacity parameter [{'capacity': 700, 'id': 1, 'name': 'AMD'}, {'capacity': 2000, 'id': 2, 'name': 'Honda'}, {'capacity': 2000, 'id': 4, 'name': 'Austin Ventures'}, {'capacity': 2300, 'id': 3, 'name': 'Austin Kiddie Limits'}] Also, you can sort unique keys as follows: venues =[{'capacity': 700, 'id': 1, 'name': 'AMD'}, {'capacity': 2000, 'id': 2, 'name': 'Honda'}, {'capacity': 2300, 'id': 3, 'name': 'Austin Kiddie Limits'}, {'capacity': 2000, 'id': 4, 'name': 'Austin Ventures'}] venues.sort(key=lambda x: (x["id"], x["name"], x["capacity"])) print(venues)
To get your sort order you could use name length as the key. b = sorted(b, key=lambda x: len(x))
Populate Python dictionary with value in nested dictionary
I am using the AccuWeather RESTFul API to get the current weather conditions in the top 50 cities. One object of the JSON response looks like this: {'Key': '28143', 'LocalizedName': 'Dhaka', 'EnglishName': 'Dhaka', 'Country': {'ID': 'BD', 'LocalizedName': 'Bangladesh', 'EnglishName': 'Bangladesh'}, 'TimeZone': {'Code': 'BDT', 'Name': 'Asia/Dhaka', 'GmtOffset': 6.0, 'IsDaylightSaving': False, 'NextOffsetChange': None}, 'GeoPosition': {'Latitude': 23.7098, 'Longitude': 90.40711, 'Elevation': {'Metric': {'Value': 5.0, 'Unit': 'm', 'UnitType': 5}, 'Imperial': {'Value': 16.0, 'Unit': 'ft', 'UnitType': 0}}}, 'LocalObservationDateTime': '2021-10-09T13:11:00+06:00', 'EpochTime': 1633763460, 'WeatherText': 'Mostly cloudy', 'WeatherIcon': 6, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': {'Metric': {'Value': 32.2, 'Unit': 'C', 'UnitType': 17}, 'Imperial': {'Value': 90.0, 'Unit': 'F', 'UnitType': 18}}, 'MobileLink': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us', 'Link': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us'} Now I want to populate a dictionary with 1) "EnglishName", 2) "WeatherText", and 3) "Temperature (Celsius)". I do manage to get a key-value pair with "EnglishName" and "WeatherText" as below: weatherResponse = result.json() mydictionary = dict() for p in weatherResponse: print(p["EnglishName"]) print(p["LocalObservationDateTime"]) print(p["WeatherText"]) temp_C = list(p["Temperature"]["Metric"].values())[0] print(f"Temperature in Celsius: {temp_C}") print("--------") mydictionary[p["EnglishName"]] = p["WeatherText"] How can I assign the "temp_C" value of each key to the dictionary as well? I tried the append function but that does not work. Any help is appreciated!
Instead of adding only one value p["WeatherText"] to your dictionary you can add multiple through use of tuples, like (a,b). Please see the below line. mydictionary[p["EnglishName"]] = (p["WeatherText"], p["Temperature"]["Metric"]["Value"]) This above line you can use to assign to your dictionary key multiple values, sample output from this: {'Dhaka': ('Mostly cloudy', 32.2)} You can read tuples just like lists mydictionary["Dhaka"][0] # This for getting the text mydictionary["Dhaka"][1] # This for getting the value Also tuples may look similar to lists but in this case it is recommended to use tuples because lists should store same data type values and tuples can store multiple datatype values.
I want to populate a dictionary with 1) "EnglishName", 2) "WeatherText", and 3) "Temperature (Celsius)". See below data = [{ 'Key': '28143', 'LocalizedName': 'Dhaka', 'EnglishName': 'Dhaka', 'Country': { 'ID': 'BD', 'LocalizedName': 'Bangladesh', 'EnglishName': 'Bangladesh' }, 'TimeZone': { 'Code': 'BDT', 'Name': 'Asia/Dhaka', 'GmtOffset': 6.0, 'IsDaylightSaving': False, 'NextOffsetChange': None }, 'GeoPosition': { 'Latitude': 23.7098, 'Longitude': 90.40711, 'Elevation': { 'Metric': { 'Value': 5.0, 'Unit': 'm', 'UnitType': 5 }, 'Imperial': { 'Value': 16.0, 'Unit': 'ft', 'UnitType': 0 } } }, 'LocalObservationDateTime': '2021-10-09T13:11:00+06:00', 'EpochTime': 1633763460, 'WeatherText': 'Mostly cloudy', 'WeatherIcon': 6, 'HasPrecipitation': False, 'PrecipitationType': None, 'IsDayTime': True, 'Temperature': { 'Metric': { 'Value': 32.2, 'Unit': 'C', 'UnitType': 17 }, 'Imperial': { 'Value': 90.0, 'Unit': 'F', 'UnitType': 18 } }, 'MobileLink': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us', 'Link': 'http://www.accuweather.com/en/bd/dhaka/28143/current-weather/28143?lang=en-us' }] filtered_data = [{'EnglishName':e.get('EnglishName','NA'),'WeatherText':e.get('WeatherText','NA'),'temp_C':e.get('Temperature').get('Metric').get('Value')} for e in data] print(filtered_data) output [{'EnglishName': 'Dhaka', 'WeatherText': 'Mostly cloudy', 'temp_C': 32.2}]
folium, plugins.TimestampedGeoJson, "Time not avaliable"
I want to draw Timestamped GeoJSON using plugins.TimestampedGeoJson. But my time bar in folium map shows "Time not available" and doesn't work. I cannot find which part of my variables are wrong. Variables I used are as follows. How can I make my time bar work? points [{'coordinates': [[37.4725028, 126.4313798], [37.478525899999994, 126.6663152], [37.561648999999996, 126.79433700000001]], 'popup': 1, 'time': '2020-01-19'}, {'coordinates': [[37.679657, 126.763737], [37.4725028, 126.4313798], [37.0796065, 127.0561043], [37.0220402, 126.8134938], [37.557176, 127.00768799999999]], 'popup': 1, 'time': '2020-01-20'}, {'coordinates': [[37.673517, 126.7553], [37.670964, 126.761146], [37.679657, 126.763737], [37.520878, 127.02286299999999], [37.524661, 127.028002], [37.503137, 127.04909099999999], [37.0520115, 126.91724440000002], [37.463504, 126.65055600000001]], 'popup': 1, 'time': '2020-01-21'}, {'coordinates': [[37.560362700000006, 126.776299], [37.567226899999994, 126.75337079999998], [37.549605299999996, 126.86608829999999], [37.567226899999994, 126.75337079999998], [37.5672454, 127.00347020000001], [37.524661, 127.028002], [37.530772, 127.031924], [37.503137, 127.04909099999999], [37.0220402, 126.8134938], [37.523118200000006, 127.03281489999999], [37.555136, 126.97048899999999]], 'popup': 1, 'time': '2020-01-22'}, {'coordinates': [[37.524703, 127.015943], [37.500735, 127.036373], [37.494607, 127.06329199999999], [37.503137, 127.04909099999999], [37.0220402, 126.8134938], [37.483702, 126.77811299999999]], 'popup': 1, 'time': '2020-01-23'}, {'coordinates': [[37.524661, 127.028002], [37.658513, 126.832025], [37.674671999999994, 126.776701], [37.678166, 126.812165], [37.0220402, 126.8134938], [37.5616902, 126.97456809999998], [37.266184, 126.999655], [37.263417, 127.028654], [37.361576, 126.935174]], 'popup': 1, 'time': '2020-01-24'}, {'coordinates': [[37.679657, 126.763737], [37.642371999999995, 126.831253], [37.0520115, 126.91724440000002], [37.5616902, 126.97456809999998], [37.483538, 127.032643], [35.967625, 126.73678899999999], [35.967625, 126.73678899999999], [37.359123, 126.93095500000001], [37.359123, 126.93095500000001]], 'popup': 1, 'time': '2020-01-25'}, {'coordinates': [[37.35132410000001, 127.12124329999999], [37.5917891, 127.0164831], [37.564001, 127.02953500000001], [37.5903342, 127.01303200000001], [37.590492100000006, 127.0119803], [37.590611700000004, 126.9441293], [37.5863425, 126.99763390000001], [37.5616902, 126.97456809999998], [35.9867, 126.70813000000001]], 'popup': 1, 'time': '2020-01-26'}, {'coordinates': [[37.5920615, 127.01670959999998], [37.590611700000004, 126.9441293], [37.5921286, 126.98387890000001], [37.5863425, 126.99763390000001], [37.5863425, 126.99763390000001], [37.5863425, 126.99763390000001], [37.5616902, 126.97456809999998], [35.968089, 126.716128], [37.557176, 127.00768799999999]], 'popup': 1, 'time': '2020-01-27'}, {'coordinates': [[37.5916736, 127.016226], [37.5854777, 127.08637140000002], [37.5982157, 127.0797739], [37.5236782, 127.04434930000001], [37.60656420000001, 127.09043], [37.5616902, 126.97456809999998], [35.954685, 126.71244399999999], [37.483702, 126.77811299999999]], 'popup': 1, 'time': '2020-01-28'}, {'coordinates': [[37.594741799999994, 127.0728561], [37.60656420000001, 127.09043], [37.5616902, 126.97456809999998], [35.976046000000004, 126.705522], [35.982751, 126.734844]], 'popup': 1, 'time': '2020-01-29'}, {'coordinates': [[37.5647424, 126.99496140000001], [37.579669, 126.99897], [35.964349, 126.959676], [37.641158000000004, 126.791979], [37.5863425, 126.99763390000001], [37.641158000000004, 126.791979], [37.5863425, 126.99763390000001], [37.498415, 126.762864]], 'popup': 1, 'time': '2020-01-30'}, {'coordinates': [[35.964349, 126.959676], [37.5673125, 126.9706395], [37.579669, 126.99897], [37.579669, 126.99897], [37.561648, 126.7855822], [37.481458, 126.7804963]], 'popup': 1, 'time': '2020-01-31'}, {'coordinates': [[37.351375, 127.123411], [37.481458, 126.7804963], [37.304349, 127.0079881], [37.391714799999995, 127.147098]], 'popup': 1, 'time': '2020-02-01'}, {'coordinates': [[37.5672412, 127.00347020000001], [37.351375, 127.123411]], 'popup': 1, 'time': '2020-02-02'}] Below is my features variable. features [{'geometry': {'coordinates': [[37.4725028, 126.4313798], [37.478525899999994, 126.6663152], [37.561648999999996, 126.79433700000001]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-19'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.679657, 126.763737], [37.4725028, 126.4313798], [37.0796065, 127.0561043], [37.0220402, 126.8134938], [37.557176, 127.00768799999999]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-20'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.673517, 126.7553], [37.670964, 126.761146], [37.679657, 126.763737], [37.520878, 127.02286299999999], [37.524661, 127.028002], [37.503137, 127.04909099999999], [37.0520115, 126.91724440000002], [37.463504, 126.65055600000001]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-21'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.560362700000006, 126.776299], [37.567226899999994, 126.75337079999998], [37.549605299999996, 126.86608829999999], [37.567226899999994, 126.75337079999998], [37.5672454, 127.00347020000001], [37.524661, 127.028002], [37.530772, 127.031924], [37.503137, 127.04909099999999], [37.0220402, 126.8134938], [37.523118200000006, 127.03281489999999], [37.555136, 126.97048899999999]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-22'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.524703, 127.015943], [37.500735, 127.036373], [37.494607, 127.06329199999999], [37.503137, 127.04909099999999], [37.0220402, 126.8134938], [37.483702, 126.77811299999999]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-23'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.524661, 127.028002], [37.658513, 126.832025], [37.674671999999994, 126.776701], [37.678166, 126.812165], [37.0220402, 126.8134938], [37.5616902, 126.97456809999998], [37.266184, 126.999655], [37.263417, 127.028654], [37.361576, 126.935174]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-24'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.679657, 126.763737], [37.642371999999995, 126.831253], [37.0520115, 126.91724440000002], [37.5616902, 126.97456809999998], [37.483538, 127.032643], [35.967625, 126.73678899999999], [35.967625, 126.73678899999999], [37.359123, 126.93095500000001], [37.359123, 126.93095500000001]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-25'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.35132410000001, 127.12124329999999], [37.5917891, 127.0164831], [37.564001, 127.02953500000001], [37.5903342, 127.01303200000001], [37.590492100000006, 127.0119803], [37.590611700000004, 126.9441293], [37.5863425, 126.99763390000001], [37.5616902, 126.97456809999998], [35.9867, 126.70813000000001]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-26'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.5920615, 127.01670959999998], [37.590611700000004, 126.9441293], [37.5921286, 126.98387890000001], [37.5863425, 126.99763390000001], [37.5863425, 126.99763390000001], [37.5863425, 126.99763390000001], [37.5616902, 126.97456809999998], [35.968089, 126.716128], [37.557176, 127.00768799999999]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-27'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.5916736, 127.016226], [37.5854777, 127.08637140000002], [37.5982157, 127.0797739], [37.5236782, 127.04434930000001], [37.60656420000001, 127.09043], [37.5616902, 126.97456809999998], [35.954685, 126.71244399999999], [37.483702, 126.77811299999999]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-28'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.594741799999994, 127.0728561], [37.60656420000001, 127.09043], [37.5616902, 126.97456809999998], [35.976046000000004, 126.705522], [35.982751, 126.734844]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-29'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.5647424, 126.99496140000001], [37.579669, 126.99897], [35.964349, 126.959676], [37.641158000000004, 126.791979], [37.5863425, 126.99763390000001], [37.641158000000004, 126.791979], [37.5863425, 126.99763390000001], [37.498415, 126.762864]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-30'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[35.964349, 126.959676], [37.5673125, 126.9706395], [37.579669, 126.99897], [37.579669, 126.99897], [37.561648, 126.7855822], [37.481458, 126.7804963]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-01-31'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.351375, 127.123411], [37.481458, 126.7804963], [37.304349, 127.0079881], [37.391714799999995, 127.147098]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-02-01'}, 'type': 'Feature'}, {'geometry': {'coordinates': [[37.5672412, 127.00347020000001], [37.351375, 127.123411]], 'type': 'Point'}, 'properties': {'icon': 'marker', 'iconstyle': {'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png'}, 'id': 'house', 'popup': 1, 'time': '2020-02-02'}, 'type': 'Feature'}] I tried to draw folium map. But my time lower left in map left doesn't work. m = folium.Map([37.5650172,126.8494648], zoom_start = 10) plugins.TimestampedGeoJson( { 'type': 'FeatureCollection', 'features': features }, period='P1D', add_last_point=True, auto_play=False, loop=False, max_speed=1, loop_button=True, date_options='YYYY-MM-DD', time_slider_drag_update=True, duration='P1D' ).add_to(m) m
I had the same problem, which was solved by the following in my case: The issue you described occurs likely because your sub-dict-keyword "time" doesn't contain a list of equal length to the employed "coordinates" in each "feature". Also, I suggest you to rename "time" to "times", as you will see in the original post on GitHub mentioned below. Hereafter, I made a minimal example based on your provided feature list: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # * Example of correct feature list with length 1 * # ## Coordinates coordinate_list1 = [[37.4725028, 126.4313798], [37.478525899999994, 126.6663152], [37.561648999999996, 126.79433700000001]] ## Times # NOTE: if the same timestamp applies to all coordinates, create list with repeating timestamp of same length as coordinate list times_list1 = ['2020-01-19'] * len(coordinate_list1) ## Features # NOTE: fill in the coordinates and times, and name the associated time-keyword "times" features = [{ 'geometry': { 'coordinates': coordinate_list1, 'type': 'Point' }, 'properties': { 'icon': 'marker', 'iconstyle': { 'iconSize': [20, 20], 'iconUrl': 'http://downloadicons.net/sites/default/files/small-house-with-a-chimney-icon-70053.png' }, 'id': 'house', 'popup': 1, 'times': times_list1 }, 'type': 'Feature' }] Fix this and it should work just fine. The solution was cited from this discussion: [...] the plugin requires each feature to have a property "times", which should be a list containing either unix epoch timestamps in ms or ISO format timestamps. See https://python-visualization.github.io/folium/plugins.html#folium.plugins.TimestampedGeoJson
df = pd.read_excel("file.xlsx") IconUrl = "https://cdn.icon-icons.com/icons2/1465/PNG/128/604bullettrain_100995.png" m = folium.Map([37.5650172,126.8494648], zoom_start = 10) # step 1 TempFeature = [] # do the following command for each marker temp_df = df.query(f"Marker_id == {1}") TempFeature.append({ 'type': 'Feature', 'geometry': { 'type': 'LineString', # example coordinates # [ # (-3.049648, 53.4372116), # (-3.04967139134615, 53.4372056616587), # (-3.04972986971154, 53.4371908158053), # . # . # . # (-3.04972986971154, 53.4371908158053), # ] 'coordinates': list(zip(temp_df['Longitude'],temp_df['Latitude'])) }, 'properties':{ 'icon': 'marker', 'iconstyle': {'iconUrl': IconUrl, 'iconSize': [20, 20], }, # time must be like the following format : '2018-12-01T05:53:00' 'times': temp_df['Timetable'] , 'popup': '<html> <head></head> <body> comments </body> </html>' }}) # step 2 TimestampedGeoJson({ 'type': 'FeatureCollection', 'features': TempFeature, } , period='P1D' ).add_to(m) m
Dictionary of Dictionaries : Sorting by a specific key
I have a dictionary that looks like this {'Africa': {'Name': 'Africa', 'men': 33333, 'priority': 3, 'women': 30000}, 'America': {'Name': 'USA', 'men': 1114444411333L, 'priority': 4, 'women': 44430000}, 'Asia': {'Name': 'China', 'men': 444433333, 'priority': 2, 'women': 444430000}, 'Europe': {'Name': 'UK', 'men': 11111333, 'priority': 1, 'women': 1111430000}} I need to sort this dictionary by Key = Priority I'm using 2.7 and have tried few options (which dont look very elegant). Any suggestions?
>>> d = {"Africa" : { "Name" : "Africa", "men": 33333, "women" : 30000, "priority" :3}, "Asia": { "Name" : "China", "men": 444433333, "women" : 444430000, "priority" :2}, "Europe": { "Name" : "UK", "men": 11111333, "women" : 1111430000, "priority" :1}, "America": { "Name" : "USA", "men": 1114444411333, "women" : 44430000, "priority" :4} } >>> from collections import OrderedDict >>> OrderedDict(sorted(d.items(), key=lambda x: x[1]['priority'])) OrderedDict([('Europe', {'priority': 1, 'men': 11111333, 'Name': 'UK', 'women': 1111430000}), ('Asia', {'priority': 2, 'men': 444433333, 'Name': 'China', 'women': 444430000}), ('Africa', {'priority': 3, 'men': 33333, 'Name': 'Africa', 'women': 30000}), ('America', {'priority': 4, 'men': 1114444411333L, 'Name': 'USA', 'women': 44430000})])
It is not possible to sort a dict.You can't get a dictionary as sorted, but you can convert it to sorted tuple list. Here is another version of sorting it; data={'Africa': {'Name': 'Africa', 'men': 33333, 'priority': 3, 'women': 30000}, 'America': {'Name': 'USA', 'men': 1114444411333L, 'priority': 4, 'women': 44430000}, 'Asia': {'Name': 'China', 'men': 444433333, 'priority': 2, 'women': 444430000}, 'Europe': {'Name': 'UK', 'men': 11111333, 'priority': 1, 'women': 1111430000}} from operator import itemgetter listOfTuple = sorted(data.items(), key= lambda(k,v):itemgetter(1)('priority')) listOfTuple.sort(key=lambda tup:tup[1]['priority']) print listOfTuple >>>[('Europe', {'priority': 1, 'men': 11111333, 'Name': 'UK', 'women': 1111430000}), ('Asia', {'priority': 2, 'men': 444433333, 'Name': 'China', 'women': 444430000}), ('Africa', {'priority': 3, 'men': 33333, 'Name': 'Africa', 'women': 30000}), ('America', {'priority': 4, 'men': 1114444411333L, 'Name': 'USA', 'women': 44430000})]
Find a minimal value in each array in Python
Suppose I have the following data in Python 3.3: my_array = [{'man_id': 1, '_id': ObjectId('1234566'), 'type': 'worker', 'value': 11}, {'man_id': 1, '_id': ObjectId('1234577'), 'type': 'worker', 'value': 12}], [{'man_id': 2, '_id': ObjectId('1234588'), 'type': 'worker', 'value': 11}, {'man_id': 2, '_id': ObjectId('3243'), 'type': 'worker', 'value': 7}, {'man_id': 2, '_id': ObjectId('54'), 'type': 'worker', 'value': 99}, {'man_id': 2, '_id': ObjectId('9879878'), 'type': 'worker', 'value': 135}], #............................. [{'man_id': 13, '_id': ObjectId('111'), 'type': 'worker', 'value': 1}, {'man_id': 13, '_id': ObjectId('222'), 'type': 'worker', 'value': 2}, {'man_id': 13, '_id': ObjectId('3333'), 'type': 'worker', 'value': 9}] There are 3 arrays. How do I find an element in each array with minimal value?
[min(arr, key=lambda s:s['value']) for arr in my_array]
Maybe something like that is acc for you: for arr in my_array: minVal = min([row['value'] for row in arr]) print [row for row in arr if row['value'] == minVal]