-1

I have a problem. I wanna to store each column from .csv file into list. So if I have csv file like that https://i.stack.imgur.com/WuRyt.png

And I want to store it in the way that:

ColumnA = [50-001, 50-002, 50-003, 50-004, 50-005, 50-006, 50-007, 50-008, 50-009]
ColumnB = [85-001, 85-003, 85-004 , 85-004 ,85-004 , 85-005 ,85-005, 85-006 ,85-007]

etc.

I have for now something like that, but it's store each row to list, but I need each column to the list. Can someone help me? I also tried with pandas, and also I can't do that.

csvfile = open('Kody pocztowe csv.csv', 'r')

csv1 = csv.reader(csvfile,delimiter = ',')
sort = sorted(csv1, key=operator.itemgetter(0))

for eachline in sort:
    print(eachline)

csvfile.readline()

lx = []
for line in csvfile:
    row = line.split(',')
    lx.append(row)

print(lx)
martineau
  • 119,623
  • 25
  • 170
  • 301
Jakub Pluta
  • 147
  • 5
  • 13

4 Answers4

2

Here is a non-pandas version (requires Python3). One can transpose an array using zip or the related itertools.zip_longest().

from itertools import zip_longest
import csv

# Read the data in organized by rows
with open('Kody pocztowe csv.csv') as csvfile:
    row_data = list(csv.reader(csvfile, delimiter=';'))

# Transpose data into column organization
col_data = list(zip_longest(*row_data))

# OP asked for individual column variables
ColumnA, ColumnB, ColumnC, ColumnD = col_data[:4]

print("ColumnA = ", ColumnA)
print("ColumnB = ", ColumnB)
print("ColumnC = ", ColumnC)
print("ColumnD = ", ColumnD)

Input file:

00;01;02;03
10;11;12
20;21;22;23;24

Result:

ColumnA =  ('00', '10', '20')
ColumnB =  ('01', '11', '21')
ColumnC =  ('02', '12', '22')
ColumnD =  ('03', None, '23')
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

Well, I did it with pandas, here's how:

import pandas as pd
df = pd.read_csv("yourdoc.csv")
columnTitles = list(df)
listOfResults = []
for eachCol in columnTitles:
    listOfResults.append(df[eachCol].tolist())

Then you should have a list of lists, or columns.

Yunkai Xiao
  • 191
  • 12
  • Note that while this _makes use of_ pandas, it couldn't really be classed as a pandas approach; it's just making use of pandas as a convenience tool for reading in CSV data and then just defaulting back to python. You could use `df.transpose()` – roganjosh Feb 13 '18 at 21:32
0

The problem is that the delimiter is ";" and not a comma.

Make sure that row = line.split(';')

  instead of line.split('**,**')
sanrio
  • 1
0

Use numpys genfromtxt:

import numpy as np
data=np.genfromtxt('Kody pocztowe csv.csv',delimiter=';',dtype=float)
#access columns as:
column1=data[:,0].tolist()
column2=data[:,1].tolist()
...
BGraf
  • 607
  • 4
  • 12