Home > Software engineering >  Convert CSV to dictionary of dictionaries
Convert CSV to dictionary of dictionaries

Time:03-30

data = {'': '11', 'RecID': '11', 'year': '2020', 'Flugnummer': 'DL67', 'cabin_class': 'economy', 'DEPARTURE_AIRPORT': 'ZRH', 'ARRIVAL_AIRPORT': 'ATL', 'Anzahl Flugsegmente': '1', 'distance': '7652', 'currencies': 'USD', 'EMISSIONS_KGCO2EQ': '1260', 'dep_country': 'CH', 'dep_coordinates': 'c(8.548056', 'arr_country': ' 47.458056)', 'arr_coordinates': 'US', None: ['c(-84.428101', ' 33.6367)']}  

print("Payload ")
def emissions(data):  
    test_dic = {} # New dictionary
    for i in range(0, len(data)):
        if data["DEPARTURE_AIRPORT"][i] == "DEPARTURE_AIRPORT":
            return DEPARTURE_AIRPORT
        if data["ARRIVAL_AIRPORT"][i] == "ARRIVAL_AIRPORT":
            return ARRIVAL_AIRPORT
        if data["cabin_class"][i] == "cabin_class":
            return cabin_class
        test_dic.update({"segments": [{ "origin": DEPARTURE_AIRPORT, "destination": ARRIVAL_AIRPORT},], "cabin_class": cabin_class, "currencies":["USD"]})
    return payload
print(emissions(data))

Desired format: data = {"segments" : [{"origin" : "ZRH","destination" : "ATL"},],"cabin_class" : "economy","currencies" : ["USD"]}

I have this dataset above data which should be transformed into the desired format below. I tried the function above which I wrote, but it seems not to work, and whith the codes already online I couldn't figure it out. Excuse me, if I ask this question again. But I can not get it to work, to change the list/csv into the dictionary.

CodePudding user response:

defaultdict and zip(*) are you're friends for this task

from collections import defaultdict


def emissions(data):
    result = defaultdict(list)
    for org, arr, cls, cur in zip(*data.values()):
        result["segments"].append({
                "origin": org,
                "destionation": arr})
        result["cabin_class"].append(cls)
        result["currencies"].append(cur)
    return dict(result)

Explanation:

defaultdict takes a type. If a key is not present in the dict, yet, then it will generate a new value of the given type, insert into the value under the given key and then return the value. Otherwise it just returns the value associated with the given key.

zip(*) is kind of a matrix transpose operation. It switches columnss to rows. Columns are all the values of your given dataset.

  • Related