Skip to content Skip to sidebar Skip to footer

How To Return A Specific Data Structure With Inner Dictionary Of Lists

I have a csv file (image attached) and to take the CSV file and create a dictionary of lists with the format '{method},{number},{orbital_period},{mass},{distance},{year}' . So far

Solution 1:

Hey SKR01 welcome to Stackoverflow!

I would suggest working with the pandas library. It is meant for table like contents that you have there. What you are then looking for is a groupby on your #method column.

import pandas as pd

def remove_index(row):
    d = row._asdict()
    del d["Index"]
    return d


df = pd.read_csv("https://docs.google.com/uc?export=download&id=1PnQzoefx-IiB3D5BKVOrcawoVFLIPVXQ")

{row.Index : remove_index(row) for row in df.groupby('#method').aggregate(list).itertuples()}

The only thing that remains is removing the nan values from the resulting dict.


Solution 2:

If you don't want to use Pandas, maybe something like this is what you're looking for:

import csv

with open('exoplanets.csv') as inputfile : 
    reader = csv.reader(inputfile)
    inputm = list(reader)

    header = inputm.pop(0)
    del header[0] # probably you don't want "#method"

    # create and populate the final dictionary
    data = {}
    for row in inputm:
        if row[0] not in data:
            data[row[0]] = {h:[] for h in header}

        for i, h in enumerate(header):
            data[row[0]][h].append(row[i+1])

print(data)

Solution 3:

This is a bit complex, and I'm questioning why you want the data this way, but this should get you the output format you want without requiring any external libraries like Pandas.

import csv

with open('exoplanets.csv') as input_file: 
    rows = list(csv.DictReader(input_file))

    # Create the data structure
    methods = {d["#method"]: {} for d in rows}

    # Get a list of fields, trimming off the method column
    fields = list(rows[1])[1:]

    # Fill in the data structure
    for method in methods:
        methods[method] = {
            # Null-trimmed version of listcomp
            # f: [r[f] for r in rows if r["#method"] == method and r[f]]
            f: [r[f] for r in rows if r["#method"] == method]
            for f
            in fields
        }

Note: This could be one multi-tiered list/dict comprehension, but I've broken it apart for clarity.


Post a Comment for "How To Return A Specific Data Structure With Inner Dictionary Of Lists"