JohnnyW
JohnnyW

Reputation: 493

Python - append items of list to different keys in dictionary

I have data from a .csv file that is as follows (the real file contains much more data). The first row that I'm showing contains the labels for each column, and the rest of the lines contain temperature data for each location. There is also a header before this data that I did not include so the top row of the .csv file does not contain the labels.

Scan,location1,location2,location3
1,32.621,38.57,36.977
2,30.976,38.451,36.971
3,32.599,38.536,36.991

What I want to do is to create a dictionary of lists where the keys are the labels (top row) and the value is a list of all temperatures for each label. So the output I'm looking for is this...

{"scan":[1,2,3], "location1":[32.621,30.976,32.599], "location2":[38.57,38.451,38.536], "location3":[36.977,36.971,36.991]}

I have successfully created a list of the keys and a dictionary which contains empty lists as values. I am having a problem in me 'else' statement where all temperature data on each line goes into each key of the dictionary. How can I get each element of the line_data list to be appended to a different key in my data dictionary?

import csv
from tkinter.filedialog import askopenfilename

csv_file_name = askopenfilename(title='Select the temperature file you want to analyze')
file = open(csv_file_name, 'r')
read_file = file.readlines()
keys = []
for line in read_file:
    # Column titles will be dictionary keys
    if keys == []:
        keys = line.split(",")
        data = dict.fromkeys(keys, [])
    else:
        line_data = line.split(",")
        for i in range(len(line_data)):
            data[keys[i]].append(line_data[i])

Upvotes: 0

Views: 163

Answers (3)

Bharat Gera
Bharat Gera

Reputation: 820

Going with your approach, You are passing [] to dict.fromkeys function which means all values in the dictionary will be pointing to same list object.

Find the below code to make it correct:

import csv
from tkinter.filedialog import askopenfilename

csv_file_name = askopenfilename(title='Select the temperature file you want to analyze')
file = open(csv_file_name, 'r')
read_file = file.readlines()
keys = []
for line in read_file:
    # Column titles will be dictionary keys
    if keys == []:
        keys = line.split(",")
        data={keys[k]:[] for k in range(len(keys))} # **you can initialise a dict like this**
        # data = dict.fromkeys(keys, []) # -- Don't do this!!
    else:
        line_data = line.split(",")
        for i in range(len(line_data)):
            data[keys[i]].append(line_data[i])
print(data)

Hope this helps!

Upvotes: 1

Humi
Humi

Reputation: 609

Is this what you are looking for?

df
Out[9]: 
   Scan  location1  location2  location3
0     1     32.621     38.570     36.977
1     2     30.976     38.451     36.971

df.to_dict('list')
Out[12]: 
{'Scan': [1, 2],
 'location1': [32.621, 30.976],
 'location2': [38.57, 38.451],
 'location3': [36.977, 36.971]}

Upvotes: 0

Akash Pagar
Akash Pagar

Reputation: 637

Use pandas to read CSV file and further can be processed through dictionary processing.

import pandas as pd

df = pd.read_csv('sales.csv')

res_dict = {col:list(df[col].values) for col in df.columns}

print(res_dict)

Output:

{'Scan': [1, 2, 3], 'location1': [32.621, 30.976, 32.599000000000004], 'location2': [38.57, 38.451, 38.536], 'location3': [36.977, 36.971, 36.991]}

Upvotes: 2

Related Questions