How to iterate and group through dictionary in python - python
I am fighting with the query as I have to count What are the total CO2 emissions by each Airline?
I have managed to get all the data to a dictionary which looks like
Flight Number is 2HX and the airline is IT and the aircraft is E195 going from EDDF to LIMF
Flight distance is 542.93 km
Flight CO2 emissions is 16.87 kg
Flight Number is 8031 and the airline is ES and the aircraft is B752 going from LEBL to EDDP
Flight distance is 1365.97 km
Flight CO2 emissions is 31.07 kg
Flight Number is 39DV and the airline is ES and the aircraft is A320 going from LEPA to LEMD
Flight distance is 546.33 km
Flight CO2 emissions is 16.92 kg
All calculations are done by all of the flights but I would like to group them based on the AIRLINE, thus increasing the total results for them and printing them accordingly.
Any ideas how I could start it?
JSON file loaded looks like this
[{"hex": "150694", "reg_number": "RA-67220", "flag": "RU", "lat": 51.633911, "lng": 50.050518, "alt": 11582, "dir": 290, "speed": 761, "v_speed": 0.3, "squawk": "0507", "flight_number": "9004", "flight_icao": "TUL9004", "dep_icao": "VIDP", "dep_iata": "DEL", "airline_icao": "PLG", "aircraft_icao": "CRJ2", "updated": 1675528289, "status": "en-route"}, {"hex": "152038", "reg_number": "RA-73784", "flag": "RU", "lat": 43.352108, "lng": 35.634342, "alt": 11277, "dir": 4, "speed": 881, "v_speed": 0, "squawk": "7313", "flight_number": "427", "flight_icao": "AFL427", "flight_iata": "SU427", "dep_icao": "HESH", "dep_iata": "SSH", "arr_icao": "UUEE", "arr_iata": "SVO", "airline_icao": "AFL", "airline_iata": "SU", "aircraft_icao": "A333", "updated": 1675528054, "status": "en-route"}, {"hex": "152052", "reg_number": "RA-73810", "flag": "RU", "lat": 59.739784, "lng": 85.652138, "alt": 9745, "dir": 89, "speed": 801, "v_speed": 0, "squawk": "5521", "flight_number": "173", "flight_icao": "SVR173", "flight_iata": "U6173", "dep_icao": "USSS", "dep_iata": "SVX", "arr_icao": "UHHH", "arr_iata": "KHV", "airline_icao": "SVR", "airline_iata": "U6", "aircraft_icao": "A319", "updated": 1675528294, "status": "en-route"}
Basically function for listing flights look like this, but I would like to group them by airlines and add value of co2 emissions to each of individual results
def list_all_flights(self):
#List all flights
total_result = 0
for i in self.flights_list.read_data_file(): # json.file
if(i.get('dep_icao') and i.get('arr_icao')):
print(f"Flight Number is {i['flight_number']} and the airline is {i['flag']} and the aircraft is {i['aircraft_icao']} going from {i['dep_icao']} to {i['arr_icao']}");
I have managed to count all encounters of different airline inside new dictionary and it works
if 'flag' in i:
temp[i['flag']] = temp.get(i['flag'], 0) + 1'
Now I would like to add the result for co2 emissions as a total for an airline.
By making use of Pandas module (you can install via pip install pandas) I made this.
import pandas as pd
df = pd.read_json("data.json")
result = {a_iata:[] for a_iata in df.airline_iata.unique()}
for a_iata in result:
result[a_iata] = df.loc[df.airline_iata == a_iata]
Where data.json is the data that you have provided. The code essentially filtrates every entry by its airline_iata value and stores them into individual DataFrames. You can see the data by using result['AIRLINE_CODE'] and it will return the DataFrame.
When you are constructing your message, you can use something like this:
temp_df = result['AIRLINE_CODE']
message = f"Flight reg number is {temp_df.reg_number}..."
You can fill the message out however you like.
Related
Pass text to a Python script and return the result using R JSON
I have a string in R that I would like to pass to python in order to compute something and return the result back into R. I have the following which "works" but not as I would like. The below passes a string from R, to a Python file, uses openAI to collect the text data and then load it back into R. library(reticulate) computePythonFunction <- " def print_openai_response(): import openai openai.api_key = 'ej-powurjf___OpenAI_API_KEY___HGAJjswe' # you will need an API key prompt = 'write me a poem about the sea' response = openai.Completion.create(engine = 'text-davinci-003', prompt = prompt, max_tokens=1000) #response['choices'][0]['text'] print(response) " py_run_string(computePythonFunction) py$print_openai_response() library("rjson") fromJSON(as.character(py$print_openai_response())) I would like to store the results in R objects - i.e. Here is one output from the python script. { "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "text": "\n\nThe sea glitters like stars in the night \nWith hues, vibrant and bright\nThe waves flow gentle, serene, and divine \nLike the sun's most gentle shine\n\nAs the sea reaches, so wide, so vast \nAn adventure awaits, and a pleasure, not passed\nWhite sands, with seaweed green \nForms a kingdom of the sea\n\nConnecting different land and tide \nThe sea churns, dancing with the sun's pride\nAs a tempest forms, raging and wild \nThe sea turns, its colors so mild\n\nA raging storm, so wild and deep \nProtecting the creatures that no one can see \nThe sea is a living breathing soul \nA true and untouchable goal \n\nThe sea is a beauty that no one can describe \nAnd it's power, no one can deny \nAn ever-lasting bond, timeless and free \nThe love of the sea, is a love, to keep" } ], "created": 1670525403, "id": "cmpl-6LGG3hDNzeTZ5VFbkyjwfkHH7rDkE", "model": "text-davinci-003", "object": "text_completion", "usage": { "completion_tokens": 210, "prompt_tokens": 7, "total_tokens": 217 } } I am interested in the text generated but I am also interested in the completion_tokens, promt_tokens and total_tokens. I thought about save the Python code as a script, then pass the argument to it such as: myPythin.py arg1. How can I return the JSON output from the model to an R object? The only input which changes/varies in the python code is the prompt variable.
Trying to sum Python list [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 1 year ago. Improve this question this is my first post here, I am learning and practicing Python. The problem is that anything I try to run after a for loop not running, so at the end I can’t get a total count. Maybe I should have created a function but now I need to know why this is happening, what have I done wrong? lst = ["32.225.012", "US", "574.280", "17.997.267", "India", "201.187", "14.521.289", "Brazil", "398.185", "5.626.942", "France", "104.077", "4.751.026", "Turkey", "39.398", "4.732.981", "Russia", "107.547", "4.427.433", "United Kingdom", "127.734", "3.994.894", "Italy", "120.256", "3.504.799", "Spain", "77.943", "3.351.014", "Germany", "82.395", "2.905.172", "Argentina", "62.599", "2.824.626", "Colombia", "72.725", "2.776.927", "Poland", "66.533", "2.459.906", "Iran", "70.966", "2.333.126", "Mexico", "215.547", "2.102.130", "Ukraine", "45.211", "1.775.062", "Peru", "60.416", "1.657.035", "Indonesia", "45.116", "1.626.033", "Czechia", "29.141", "1.578.450", "South Africa", "54.285", "1.506.455", "Netherlands", "17.339", "1.210.077", "Canada", "24.105", "1.184.271", "Chile", "26.073", "1.051.868", "Iraq", "15.392", "1.051.779", "Romania", "27.833", "1.020.495", "Philippines", "17.031", "979.034", "Belgium", "24.104", "960.520", "Sweden", "14.000", "838.323", "Israel", "6.361", "835.563", "Portugal", "16.973", "810.231", "Pakistan", "17.530", "774.399", "Hungary", "27.172", "754.614", "Bangladesh", "11.305", "708.265", "Jordan", "8.754", "685.937", "Serbia", "6.312", "656.077", "Switzerland", "10.617", "614.510", "Austria", "10.152", "580.666", "Japan", "10.052", "524.241", "Lebanon", "7.224", "516.301", "United Arab Emirates", "1.580", "510.465", "Morocco", "9.015", "415.281", "Saudi Arabia", "6.935", "402.491", "Bulgaria", "16.278", "401.593", "Malaysia", "1.477", "381.180", "Slovakia", "11.611", "377.662", "Ecuador", "18.470", "366.709", "Kazakhstan", "3.326", "363.533", "Panama", "6.216", "355.924", "Belarus", "2.522", "340.493", "Greece", "10.242", "327.737", "Croatia", "7.001", "316.521", "Azerbaijan", "4.461", "312.699", "Nepal", "3.211","307.401", "Georgia", "4.077", "305.313", "Tunisia", "10.563", "300.258", "Bolivia", "12.885", "294.550", "West Bank and Gaza", "3.206", "271.814", "Paraguay", "6.094", "271.145", "Kuwait", "1.546", "265.819", "Dominican Republic", "3.467", "255.288", "Ethiopia", "3.639", "250.479", "Denmark", "2.482", "250.138", "Moldova", "5.780", "247.857", "Ireland", "4.896", "244.555", "Lithuania", "3.900", "243.167", "Costa Rica", "3.186", "238.421", "Slovenia", "4.236", "224.621", "Guatemala", "7.478", "224.517", "Egypt", "13.168", "214.872", "Armenia", "4.071", "208.356", "Honduras", "5.212", "204.289", "Qatar", "445","197.378", "Bosnia and Herzegovina", "8.464", "193.721", "Venezuela", "2.082", "192.326", "Oman", "2.001","190.096", "Uruguay", "2.452", "176.701", "Libya", "3.019","174.659", "Bahrain", "632","164.912", "Nigeria", "2.063", "158.326", "Kenya", "2.688","151.569", "North Macedonia", "4.772", "142.790", "Burma", "3.209","130.859", "Albania", "2.386", "121.580", "Algeria", "3.234", "121.232", "Estonia", "1.148", "120.673", "Korea. South", "1.821", "117.099", "Latvia", "2.118", "111.915", "Norway", "753","104.953", "Sri Lanka", "661", "104.512", "Cuba", "614","103.638", "Kosovo", "2.134", "102.426", "China", "4.845","97.080", "Montenegro", "1.485", "94.599", "Kyrgyzstan", "1.592", "92.513", "Ghana", "779","91.484", "Zambia", "1.249","90.008", "Uzbekistan", "646", "86.405", "Finland", "908","69.804", "Mozambique", "814", "68.922", "El Salvador", "2.117", "66.826", "Luxembourg", "792", "65.998", "Cameroon", "991","63.720", "Cyprus", "303","61.699", "Thailand", "178","61.086", "Singapore", "30","59.370", "Afghanistan", "2.611", "48.177", "Namibia", "638","46.600", "Botswana", "702","45.885", "Cote d'Ivoire", "285", "45.292", "Jamaica", "770","41.766", "Uganda", "341","40.249", "Senegal", "1.107", "38.191", "Zimbabwe", "1.565", "36.510", "Madagascar", "631", "34.052", "Malawi", "1.147","33.944", "Sudan", "2.349","33.608", "Mongolia", "97","30.249", "Malta", "413","29.768", "Congo Kinshasa", "763", "29.749", "Australia", "910", "29.052", "Maldives", "72","25.942", "Angola", "587","24.888", "Rwanda", "332","23.181", "Cabo Verde", "213", "22.568", "Gabon", "138","22.513", "Syria", "1.572","22.087", "Guinea", "141","18.452", "Eswatini", "671","18.314", "Mauritania", "455", "13.915", "Somalia", "713","13.780", "Mali", "477","13.308", "Tajikistan", "90", "13.286", "Burkina Faso", "157", "13.148", "Andorra", "125","13.017", "Haiti", "254","12.963", "Guyana", "293","12.898", "Togo", "122","12.631", "Belize", "322","11.761", "Cambodia", "88","10.986", "Djibouti", "142","10.915", "Papua New Guinea", "107", "10.730", "Lesotho", "316","10.678", "Congo Brazzaville", "144", "10.553", "South Sudan", "114", "10.220", "Bahamas", "198","10.170", "Trinidad and Tobago", "163", "10.157", "Suriname", "201","7.821", "Benin", "99","7.559", "Equatorial Guinea", "107", "6.898", "Nicaragua", "182","6.456", "Iceland", "29","6.359", "Central African Republic", "87", "6.220", "Yemen", "1.207","5.882", "Gambia", "174","5.354", "Seychelles", "26","5.220", "Niger", "191","5.059", "San Marino", "90","4.789", "Chad", "170","4.508", "Saint Lucia", "74", "4.049", "Sierra Leone", "79", "3.941", "Burundi", "6","3.833", "Comoros", "146","3.831", "Barbados", "44","3.731", "Guinea-Bissau", "67", "3.659", "Eritrea", "10","2.908", "Liechtenstein", "57", "2.865", "Vietnam", "35","2.610", "New Zealand", "26", "2.447", "Monaco", "32","2.301", "Sao Tome and Principe", "35", "2.124", "Timor-Leste", "3","2.099", "Liberia", "85","1.850", "Saint Vincent and the Grenadines", "11", "1.232", "Antigua and Barbuda", "32", "1.207", "Mauritius", "17","1.116", "Taiwan", "12","1.059", "Bhutan", "1","712", "Diamond Princess", "13", "604", "Laos", "0","509", "Tanzania", "21","224", "Brunei", "3","173", "Dominica", "0","159", "Grenada", "1","111", "Fiji", "2","44", "Saint Kitts and Nevis", "0", "27", "Holy See", "0","20", "Solomon Islands", "0", "9", "MS Zaandam", "2","4", "Marshall Islands", "0", "4", "Vanuatu", "1","3", "Samoa", "0","1", "Micronesia", "0"] countryIndex = 1 casesIndex = 0 deathsIndex = 2 countries = [] cases = [] deaths = [] for item in lst: print(f"Country: {lst[countryIndex]}") print(f"Cases: {lst[casesIndex]}") print(f"Deaths: {lst[deathsIndex]}") print("") countryToAppend = lst[countryIndex] casesToAppend = lst[casesIndex] deathsToAppend = lst[deathsIndex] countries.append(countryToAppend) cases.append(casesToAppend) deaths.append(deathsToAppend) countryIndex += 3 casesIndex += 3 deathsIndex += 3 total = sum(deaths) print(f"Total deaths: {total}")
On top of the suggestion to replace the name of the data set to not use the reserved word list, my recommendation would be to leverage the ability to skip in the builtin range in an example like so: # Lists to store data countries = [] total_cases = [] total_deaths = [] # Iterate over thje range of the data skipping 3 at a time: 0, 3, ... for x in range(0, len(data), 3): # Parse out the cases a deaths to ints cases = int(data[x].replace('.', '')) deaths = int(data[x+2].replace('.', '')) # We can just extract the country label country_label = data[x+1] countries.append(country_label) total_cases.append(cases) total_deaths.append(deaths) # Get the desired sums sum_cases = sum(total_cases) sum_deaths = sum(total_deaths) print(f"The total cases: {sum_cases}") print(f"The total deaths: {sum_deaths}") Above I renamed your dataset to be data and was able to sum up each list.
sum = 0 for i in range(2,len(l),3): # l is your list of data , sum = sum+int(l[i].replace('.','')) # here I removed the point between numbers ex: 574.280 --> 574280 print(sum) #output : 3145239
Changing stoploss in metatrader5 with python using Metatrader5 library and nothing happens
I have this code for changing stoploss on opened order/orders in metatrader 5. When I run this code nothing happens even when my compiler print that, nothing is wrong. I have algotrading on so I'm not sure where is the problem. Here is source code: def sl_change(ticket, SL, TP, pair, p_open, volume, o_type): order_request = { 'action': mt5.TRADE_ACTION_SLTP, 'ticket': ticket, 'type': o_type, 'price_open': p_open, 'volume': volume, 'sl': SL, 'tp': TP, 'symbol': pair, 'deviation': 20, "magic": ea_magic_number, "comment": "sent by python", "type_time": mt5.ORDER_TIME_GTC, # good till cancelled 'type_filling': mt5.ORDER_FILLING_FOK, "type_filling": mt5.ORDER_FILLING_RETURN, } result = mt5.order_check(order_request) return result, order_request pair = 'AUDUSD' SL = 0.7101 positions = mt5.positions_get(symbol=pair) ordernum = len(positions) for i in range(0, ordernum): position = positions[i] ticket = position.ticket TP = position.tp volume = position.volume o_type = position.type p_open = position.price_open print(positions) time.sleep(5) sl_change(ticket, SL, TP, pair, p_open, volume, o_type) When I replace order_check with order_send still nothing happens.
This is what works for me now it's sample code if you don't understand input answer me I can give you more info def changeslpl(ticket,pair,pos_type,SL,tp,ea_magic_number,volume,p_open): request = { "action": mt5.TRADE_ACTION_SLTP, "symbol": pair, "volume": volume, "type": pos_type, "position": ticket, "price_open": p_open, "sl": SL, "tp": tp, "deviation": 20, "magic": ea_magic_number, "comment": "python script open", "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_FOK, "ENUM_ORDER_STATE": mt5.ORDER_FILLING_RETURN, } #// perform the check and display the result 'as is' result = mt5.order_send(request) if result.retcode != mt5.TRADE_RETCODE_DONE: print("4. order_send failed, retcode={}".format(result.retcode)) print(" result",result)
I found out why we were facing this problem. When you execute a trade for the first time note that STOP_LOSS is always equal to for example 500 points difference from price but now you want to modify a stop loss. So 500 points +/- current_price = e.g. 138.500 == OUR STOP_LOSS The hack here is that you put price for a STOP_LOSS, NOT POINTS. So the new request will be: request = { 'action': mt5.TRADE_ACTION_SLTP, 'position': position.ticket, 'sl': 139.000, } Now you will be finally on to something.
You should invert your order type. If it is a mt5.ORDER_TYPE_BUY, the SL/TP modification request should be a mt5.ORDER_TYPE_SELL # This is what is important to you! if(order_type == mt5.ORDER_TYPE_BUY): order_type = mt5.ORDER_TYPE_SELL price = mt5.symbol_info_tick(symbol).bid else: order_type = mt5.ORDER_TYPE_BUY price = mt5.symbol_info_tick(symbol).ask #importance ends here. sltp_request = { "action": mt5.TRADE_ACTION_SLTP, "symbol": symbol, "volume": float(volume), "type": order_type, "position": deal_id, "sl": sl, "price": price, "magic": 234000, "comment": "Change stop loss", "type_time": mt5.ORDER_TIME_GTC, "type_filling": mt5.ORDER_FILLING_IOC, } result = mt5.order_send(sltp_request)
'DataFrame' object is not callable PYTHON
I have a code that should write information to excel using selenium. I have 1 list with some information. I need to write all this to excel, and i have solution. But, when i tried to use it i got 'DataFrame' object is not callable. How can i solve it? All this code into iteration: for schools in List: #in the List i have data from excel file with Name of schools data = pd.DataFrame() data({ "School Name":School_list_result[0::17], "Principal":School_list_result[1::17], "Principal's E-mail":School_list_result[2::17], "Type":School_list_result[8::17], "Grade Span": School_list_result[3::17], "Address":School_list_result[4::17], "Phone":School_list_result[14::17], "Website":School_list_result[13::17], "Associations/Communities":School_list_result[5::17], "GreatSchools Summary Rating":School_list_result[6::17], "U.S.News Rankings":School_list_result[12::17], "Total # Students":School_list_result[15::17], "Full-Time Teachers":School_list_result[16::17], "Student/Teacher Ratio":School_list_result[17::17], "Charter":School_list_result[9::17], "Enrollment by Race/Ethnicity": School_list_result[7::17], "Enrollment by Gender":School_list_result[10::17], "Enrollment by Grade":School_list_result[11::17], }) data.to_excel("D:\Schools.xlsx") In School_list_result i have this data: 'Cape Elizabeth High School', 'Mr. Jeffrey Shedd', 'No data.', '9-12', '345 Ocean House Road, Cape Elizabeth, ME 04107', 'Cape Elizabeth Public Schools', '8/10', 'White\n91%\nAsian\n3%\nTwo or more races\n3%\nHispanic\n3%\nBlack\n1%', 'Regular school', 'No', ' Male Female\n Students 281 252', ' 9 10 11 12\n Students 139 135 117 142', '#5,667 in National Rankings', 'https://cehs.cape.k12.me.us/', 'Tel: (207)799-3309', '516 students', '47 teachers', '11:1',
Please follow the syntax about how to create a dataframe https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html So your code should be modified as: for schools in List: #in the List i have data from excel file with Name of schools data = pd.DataFrame(data={ "School Name": School_list_result[0::17], "Principal": School_list_result[1::17], "Principal's E-mail": School_list_result[2::17], "Type": School_list_result[8::17], "Grade Span": School_list_result[3::17], "Address": School_list_result[4::17], "Phone": School_list_result[14::17], "Website": School_list_result[13::17], "Associations/Communities": School_list_result[5::17], "GreatSchools Summary Rating": School_list_result[6::17], "U.S.News Rankings": School_list_result[12::17], "Total # Students": School_list_result[15::17], "Full-Time Teachers": School_list_result[16::17], "Student/Teacher Ratio": School_list_result[17::17], "Charter": School_list_result[9::17], "Enrollment by Race/Ethnicity": School_list_result[7::17], "Enrollment by Gender": School_list_result[10::17], "Enrollment by Grade": School_list_result[11::17], })
Do you want to add in an existing xlsx file? First, create the dictionary and then call the DataFrame method, like this: r = {"column1":["data"], "column2":["data"]} data = pd.DataFrame(r)
Retrieve the country from the geographical locations in Python
I am trying to get the country name from the latitude and longitude points from my pandas dataframe. Currently I have used geolocator.reverse(latitude,longitude) to get the full address of the geographic location. But there is no option to retrieve the country name from the full address as it returns a list. Method used: def get_country(row): pos = str(row['StartLat']) + ', ' + str(row['StartLong']) locations = geolocator.reverse(pos) return locations Call to get_country by passing the dataframe: df4['country'] = df4.apply(lambda row: get_country(row), axis = 1) Current output: StartLat StartLong Address 52.509669 13.376294 Potsdamer Platz, Mitte, Berlin, Deutschland, Europe Just wondering whether there is some Python library to retrieve the country when we pass the geographic points. Any help would be appreciated.
In your get_country function, your return value location will have an attribute raw, which is a dict that looks like this: { 'address': { 'attraction': 'Potsdamer Platz', 'city': 'Berlin', 'city_district': 'Mitte', 'country': 'Deutschland', 'country_code': 'de', 'postcode': '10117', 'road': 'Potsdamer Platz', 'state': 'Berlin' }, 'boundingbox': ['52.5093982', '52.5095982', '13.3764983', '13.3766983'], 'display_name': 'Potsdamer Platz, Mitte, Berlin, 10117, Deutschland', ... and so one ... } so location.raw['address']['country'] gives 'Deutschland' If I read your question correctly, a possible solution could be: def get_country(row): pos = str(row['StartLat']) + ', ' + str(row['StartLong']) locations = geolocator.reverse(pos) return location.raw['address']['country'] EDIT: The format of the location.raw object will differ depending on which geolocator service you are using. My example uses geopy.geocoders.Nominatim, from the example on geopy's documentation site, so your results might differ.
My code,hopefully that helps: from geopy.geocoders import Nominatim nm = Nominatim() place, (lat, lng) = nm.geocode("3995 23rd st, San Francisco,CA 94114") print('Country' + ": " + place.split()[-1])
I'm not sure what service you're using with geopy, but as a small plug which I'm probably biased towards, this I think could be a simpler solution for you. https://github.com/Ziptastic/ziptastic-python from ziptastic import Ziptastic # Set API key. api = Ziptastic('<your api key>') result = api.get_from_coordinates('42.9934', '-84.1595') Which will return a list of dictionaries like so: [ { "city": "Owosso", "geohash": "dpshsfsytw8k", "country": "US", "county": "Shiawassee", "state": "Michigan", "state_short": "MI", "postal_code": "48867", "latitude": 42.9934, "longitude": -84.1595, "timezone": "America/Detroit" } ]