首页 > 解决方案 > 如何在没有熊猫的情况下将csv转换为多个数组?

问题描述

我有一个这样的 csv 文件:

student_id,event_id,score
1,1,20
3,1,20
4,1,18
5,1,13
6,1,18
7,1,14
8,1,14
9,1,11
10,1,19
...

我需要将它转换成多个数组/列表,就像我在这里使用熊猫一样:

scores = pd.read_csv("/content/score.csv", encoding = 'utf-8', 
                      index_col = [])
student_id = scores['student_id'].values
event_id = scores['event_id'].values
score = scores['score'].values
print(scores.head())

如您所见,我得到了三个数组,我需要它们来运行数据分析。如何使用 Python 的 CSV 库来做到这一点?我必须在不使用熊猫的情况下做到这一点。另外,当我处理完这些数据后,如何将多个新数组中的数据导出到 csv 文件中?我再次使用 panda 来做到这一点:

avg = avgScore
max = maxScore
min = minScore
sum = sumScore
id = student_id_data
    
dict = {'avg(score)': avg, 'max(score)': max, 'min(score)': min, 'sum(score)': sum, 'student_id': id}  
     
df = pd.DataFrame(dict) 
  
df.to_csv(r'/content/AnalyzedData.csv', index=False)

如果您想知道,前 5 个是数组。

标签: pythoncsvexport-to-csv

解决方案


这是一个部分答案,它将为 CSV 文件中的每一列生成一个单独的列表。

import csv

csv_filepath = "score.csv"

with open(csv_filepath, "r", newline='') as csv_file:
    reader = csv.DictReader(csv_file)
    columns = reader.fieldnames

    lists = {column: [] for column in columns}  # Lists for each column.

    for row in reader:
        for column in columns:
            lists[column].append(int(row[column]))

    for column_name, column in lists.items():
        print(f'{column_name}: {column}')

样本输出:

student_id: [1, 3, 4, 5, 6, 7, 8, 9, 10]
event_id: [1, 1, 1, 1, 1, 1, 1, 1, 1]
score: [20, 20, 18, 13, 18, 14, 14, 11, 19]

你还问如何做相反的事情。这是一个不言自明的例子:

# Dummy sample analysis data
length = len(lists['student_id'])
avgScore = list(range(length))
maxScore = list(range(length))
minScore = list(range(length))
sumScore = list(range(length))
student_ids = lists['student_id']

csv_output_filepath = 'analysis.csv'
fieldnames = ('avg(score)', 'max(score)', 'min(score)', 'sum(score)', 'student_id')

with open(csv_output_filepath, 'w', newline='') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames)
    writer.writeheader()

    for values in zip(avgScore, maxScore, minScore, sumScore, student_ids):
        row = dict(zip(fieldnames, values))  # Combine into dictionary.
        writer.writerow(row)

推荐阅读