Reputation: 363
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 thought 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.
Upvotes: 0
Views: 341
Reputation: 982
Consider checking out convtools library, this provides a lot of data processing primitives (brings code generation though).
from convtools import conversion as c
from convtools.contrib.tables import Table
converter = (
c.iter({(c.item("Start"), c.item("End")): c.item("Value")})
.as_type(list)
.gen_converter()
)
rows = (
Table.from_csv("input.csv", header=True)
# cast all columns to int
.update_all(int)
# or .update(Start=c.col("Start").as_type(int)) to process one by one
.into_iter_rows(dict)
)
assert converter(rows) == [{(10, 20): 30}, {(100, 334): 534}, {(122, 3456): 23}]
Upvotes: 0
Reputation: 1388
Using the csv lib.
import csv
data = []
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file)
for i, 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
Upvotes: 0
Reputation: 1218
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}
Upvotes: 1