0

I would like to turn this data into a dictionary however I want the 'name' column to be the key and only the age and height as values to be associated with the name and everything else is irrelevant.

The CSV file is arranged like so name | age | color | height | weight steven | 15 | red | 70 | 150 many other names with characteristics are below this row as well

I am new to parsing csv files and can't seem to find the framework for what I am trying to do. Is there a place I can maybe put the indexes of the columns?

with open("search_rez.csv","w+", encoding="utf-8") as out:
    out.write(final_data)
    out.close
with open('search_rez.csv', 'r') as fh:
    rd = csv.DictReader(fh, delimiter=',') 
    for row in rd:
    print(row)

A dictionary of key's(names) and values(many characteristics) that I can access and is stored into a variable. Eventually I would like to be able to search the dictionary with a given 'name' and return the values associated with it.

2
  • 1
    The question is a bit confusing. Can you add (a) example of your csv file and (b) what exactly you are trying to achieve from that csv file data. That question does not provide any information on that. Commented Jul 24, 2019 at 17:51
  • added a comment to help Commented Jul 24, 2019 at 17:59

4 Answers 4

2

I've created a dummy csv file:

import pandas as pd
import csv

test_data = {'name': ['sansa', 'bran', 'aria'], 'sex': ['F','M','F'], 'age': [15,10,12]}
test_data = pd.DataFrame(test_data)
test_data.to_csv("search_rez.csv", index=False)

This file looks like this:

data = pd.read_csv("search_rez.csv")
print(data)

    name sex  age
0  sansa   F   15
1   bran   M   10
2   aria   F   12

I've modified your code to implement the desired output (as I understood it):

my_dict = {}
with open('search_rez.csv', 'r') as fh:
    rd = csv.DictReader(fh, delimiter=',') 
    for row in rd:
        new_key = row['name']
        new_values = (row['sex'], row['age'])
        my_dict[new_key] = new_values

Which gives:

print(my_dict)

{'sansa': ('F', '15'), 'bran': ('M', '10'), 'aria': ('F', '12')}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes! This will definetely help, where you use "A", "B", etc should I use the column headers or an index?
@ubersuperuser, These are column headers, just play with it and look how the resulting csv looks like. I just saw your edit on original question, so in your case it would be the name|age| etc.
Thanks! Do i need to use the panda module at all? I saw you installed it.
@ubersuperuser, I just used it to create an example csv, since I didn't have your file.
1

The question is quite vague but I think you are looking for something like this :

df = pd.read_csv("your file here")

your_map = dict(
        df.set_index('name')
        .groupby(level=0)
        .apply(lambda x: x.to_dict(orient='records'))
)

Comments

1

You can try this:

import csv

with open('search_rez.csv') as csvfile:
    reader = csv.reader(csvfile)
    next(reader)  # Skip header 
    my_dict = {rows[0]: rows[1:] for rows in reader}

Example input:

name,D,Q,L
nikos,1,,5
maria,7,'xa',4

And the output, stored in my_dict:

{'nikos': ['1', '', '5'], 'maria': ['7', "'xa'", '4']}

Comments

0

Something like this?

>>> import csv
>>> with open("search_rez.csv", newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         if row['name'] == 'some_name':
...             print(row['columnD'], row['columnQ'])
...         

1 Comment

Not exactly, but is there a way to pre-associate the 'name' with specific column values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.