我有一个带有' n'的csv文件列。我需要获得rowcount 每列使用列名称并给出以下格式的字典:
csv_dict= {col_a:10,col_b:20,col_c:30}
其中10,20和30分别是col a,b和c的行数。 我使用Dictreader的fieldnames选项获得了一个列列表。 现在我需要列表中每列的行数。
这就是我的尝试:
for row in csv.DictReader(filename):
col_count= sum(1 for row['col_a'] in re)+1
这只是得到列a的行数。如何获取列表中所有列的行数 并以上述格式将它们放入字典中?任何帮助赞赏。谢谢和问候。
答案 0 :(得分:4)
You can try this:
#Save this file with FileName.csv
Name,age,DOB abhijeet,17,17/09/1990 raj,17,7/09/1990 ramesh,17,17/09/1990 rani,21,17/09/1990 mohan,21,17/09/1990 nil,25,17/09/1990
#Following is the python code.import csv
from collections import defaultdict
columns = defaultdict(list) # each value in each column is appended to a list
with open('FileName.csv') as f:
reader = csv.DictReader(f) # read rows into a dictionary format
for row in reader: # read a row as {column1: value1, column2: value2,...}
for (k,v) in row.items(): # go over each column name and value
if not v=='':
columns[k].append(v) # append the value into the appropriate list
# based on column name k
print len(columns['Name']) #print the length of the specified column
print len(columns['age']) #print the length of the specified column
print len(columns['DOB']) #print the length of the specified column
答案 1 :(得分:1)
我会用熊猫!
# FULLNAME= path/filename.extension of CSV file to read
data = pd.read_csv(FULLNAME, header=0)
# counting empty values
nan_values = data.isnull().sum()
# multiply by -1
ds = nan_values.multiply(-1)
# add total of rows from CSV
filled_rows = ds.add(len(data))
# create dict from data series
csv_dict = filled_rows.to_dict()
如果要保留列名称顺序,请使用OrderedDict
csv_dict_ordered = OrderedDict()
for idx in filled_rows.index:
csv_dict_ordered[idx] = filled_rows[idx]