[英]accessing the values of collections.defaultdict
I have a csv file that I want to read column wise, for that I've this code : 我有一个csv文件,我想按列阅读,因为我有以下代码:
from collections import defaultdict
from csv import DictReader
columnwise_table = defaultdict(list)
with open("Weird_stuff.csv",'rU') as f:
reader = DictReader(f)
for row in reader:
for col,dat in row.items():
columnwise_table[col].append(dat)
#print(columnwise_table.items()) # this gives me everything
print(type(columnwise_table[2]) # I'm look for smt like this
my question is how can get all the element of only one specific column ? 我的问题是如何只获取一个特定列的所有元素? and I'm not using conda and the matrix is big 2400x980
而且我没有使用conda并且矩阵很大2400x980
UPDATE UPDATE
I have 980 columns and over 2000 rows I need to work with the file using the columns say 1st column[0]: feature1
2nd column[0]: j_ss01
50th column: Abs2
and so on 我有980列,超过2000行,我需要使用
feature1
第一列[0]: feature1
第二列[0]: j_ss01
第50列: Abs2
等来处理文件
since I can't access the dict using the column names I would like to use an index for that. 因为我无法使用列名访问字典,所以我想为此使用索引。 is this possible ?
这可能吗 ?
By iterating on row.items, you get all columns. 通过对row.items进行迭代,您可以获得所有列。
If you want only one specific column via index number, use csv.reader
and column index instead. 如果您只希望通过索引号指定一列,请改用
csv.reader
和列索引。
from csv import reader
col_values = []
# Column index number to get values from
col = 1
with open("Weird_stuff.csv",'rU') as f:
reader = reader(f)
for row in reader:
col_val = row[col]
col_values.append(col_val)
# contains only values from column index <col>
print(col_values)
import csv
import collections
col_values = collections.defaultdict(list)
with open('Wierd_stuff.csv', 'rU') as f:
reader = csv.reader(f)
# skip field names
next(reader)
for row in csv.reader(f):
for col, value in enumerate(row):
col_values[col].append(value)
# for each numbered column you want...
col_index = 33 # for example
print(col_values[col_index])
If you know the columns you want in advance, only storing those columns could save you some space... 如果您知道事先想要的列,那么仅存储这些列可以节省一些空间...
cols = set(1, 5, 6, 234)
...
for col, value in enumerate(row):
if col in cols:
col_values[col].append(value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.