1

So I converted a CSV that has names in one column and values in the second column into two arrays, one array for the name and one array for the values. I want to convert this into a table that equates the two, so the first item in the name array is linked to the first item in the value. How do I go about doing this?

import csv

data = csv.reader(open('C:\\Users\\Grant\\Documents\\finalproject\\centers.csv', 'r'), delimiter=",", quotechar='|')
names, values = [], []

for row in data:
    names.append(row[0])
    values.append(row[1])
5
  • Have you tried something yet? Commented Dec 2, 2017 at 20:03
  • I've tried to turn it into a dictionary, using dict(zip(column1, zip(*column2))) but when i try and return the dictionary, nothing shows up, so i'm not sure if that is working. this is my code thus far import csv data = csv.reader(open('C:\\Users\\Grant\\Documents\\finalproject\\centers.csv', 'r'), delimiter=",", quotechar='|') column1, column2 = [], [] for row in data: column1.append(row[0]) column2.append(row[1]) centers = dict(zip(column1, zip(*column2))) print(centers) Commented Dec 2, 2017 at 20:09
  • 1
    Help us help you - share the code you have so far Commented Dec 2, 2017 at 20:10
  • sorry, just now edited the comment with my code Commented Dec 2, 2017 at 20:11
  • comments don't format well for code, edit your question and put code in there, highlight the code and press {} to format Commented Dec 2, 2017 at 20:11

2 Answers 2

2
names = ["a", "b", "c"]
values = [1, 2, 3]
for n, v in zip(names, values):
    print("{} = {}".format(n, v))
Sign up to request clarification or add additional context in comments.

2 Comments

Ah this is what I was looking for. Thank you! sorry for my poor asking of the question, i'm super new to the site and coding in general
Don't worry about it. Everyone has to start somehow :). (Would you mind accepting my answer as correct? ^^)
2

Since you say table I would suggest you go with dataframes by pandas.

Assuming your name list to be names and value list to be values,

You could have something like:

data = {'names': names, 'values': values}
df = pandas.DataFrame(data=data)

Of course you have to import pandas first.

Which will give you a "table" (or dataframe?) like:

         names          values
0      Byakuya           500
1     Kenpachi           600
2          Kon           50

Further read here.

Comments

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.