Home > Enterprise >  Create a Python dictionary with start and end values as the keys
Create a Python dictionary with start and end values as the keys

Time:10-27

What is the best way to translate this into a python dictionary or is there a better way to implement this.

I have a csv file with 3 columns. 1. Start 2. End 3. Value

eg Start, = 10, End = 90 Value = 30 ....Start=10000 End=30000 value =1

I was thinking that I could do 10:30,11:30.....10000:1 for my dictionary but how can I create this dictionary efficiently using the start and end as the keys.

Would spark be the best way?

CodePudding user response:

csv:

Start,End,Value
10,20,30
100,334,534
122,3456,23

Sample:

import pandas as pd
df = pd.read_csv('1.csv')
k = [*zip(df['Start'], df['End'])]
v = list(df['Value'])
d = dict(zip(k, v))
print(d)

Res:

{(10, 20): 30, (100, 334): 534, (122, 3456): 23}

CodePudding user response:

Using the csv lib.

import csv

data = []

with open(filename) as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in enumerate(csv_reader):
        if i == 0:  continue # skip header
        data.append({(int(row[0]), int(row[1])): int(row[2])}) # (note)

note: last int could be float depending of requested data type

  • Related