1

I would like to convert columns from my csv file into arrays in python however I want the first value in each column (the first row) to be the array names.

I have tried this:

import csv

data = csv.reader(open('products.csv', 'r'), delimiter=",", quotechar='|')
column1, column2 = [], []

for row in data:
    column1.extend(row[0])
    column2.extend(row[1])

print(column1)
print(column2)

However, this gives it character by character and does not make the array name the first value.

Here is my csv file.

name,description,price
Apples,A bag of 3 apples,1.75
White Bread,A loaf of white bread,1.90
Wholemeal Bread,A loag of wholemeal bread,1.45

It needs to be expandable (Not hardcoded/I will add to this file later)

Expected results is 3 arrays.

name = []
description = []
price = []

and the values line up e.g. index 0 in all arrays will be the first column etc etc.

3 Answers 3

4

I would highly recommend using a Pandas DataFrame for this. Do

pip3 install pandas

Then in your python code,

import pandas as pd

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

In an interactive python console, you can explore the structure of this DataFrame

%% Get the 'name' column
df['name']

%% Get the 'description' column
description = df['description']

%% Get it in a numpy array
description = df['description'].values

%% Get the row named 'Apples'
Apples = df[df['name'] == 'Apples']

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

If you have a large number of samples in csv file and don't want to construct DataFrame object, you can use csv module and constract your lists in an iterable way without loading whole data in memory:

import csv

csv_file = 'sample.csv'

names = []
description = []
price = []

with open(csv_file, 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        names.append(row.get('name'))
        description.append(row.get('description'))
        price.append(row.get('price'))

print(names)
## ['Apples', 'White Bread', 'Wholemeal Bread']

print(description)
## ['A bag of 3 apples', 'A loaf of white bread', 'A loag of wholemeal bread']

print(price)
## ['1.75', '1.90', '1.45']

Comments

0

Let's say the csv is

A, B, C
1, a, x
2, b, y
3, c, z

You can use exec to set up all the variables you need in a greater list.

Let's assume the lists are already constructed as the following

[[1, 2, 3], [a, b, c], [x, y, z]] # This should be fairly straightforward.
# data is the lists
# row is the first row
for i in range(len(row)):
   exec(row[i] + " = " + str(data[i]))

# Now all the variables should be defined 
# >>> A
# [1, 2, 3]
# ...

1 Comment

Do you know how to construct row and data in my code example?

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.