import csv
with open('Annual_Budget.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
Column_Sum = []
Third_Column_Avg = []
High_Value = []
Low_Value = []
for row in readCSV:
Column_Sum = []
Third_Column_Avg = []
High_Value = []
Low_Value = []
Column_Sum.append(Column_Sum)
Third_Column_Avg.append(Third_Column_Avg)
High_Value.append(High_Value)
Low_Value.append(Low_Value)
print(Column_Sum)
print(Third_Column_Avg)
print(High_Value)
print(Low_Value)`
如何将csv读取为文本文件,并为每一行加总所有数字列,跳过所有不能被视为数字的列,并在完成时显示总和。它还必须在第三栏中显示所有值的平均值。它还必须显示第二列中的最高值和最低值,并显示这些值出现在哪一行。我以图片格式输入了模拟年度预算,以便您可以了解我要完成的工作。
CSV SCREENSHOT EXAMPLE
输出:[SUM OF ALL NUMERIC COLUMNS], [AVERAGE OF ALL VALUES IN THIRD COLUMN], [HIGHEST VALUE FROM SECOND COLUMN][LOWEST VALUE FROM SECOND COLUMN]
答案 0 :(得分:2)
如果您没有pip install pandas
然后
In [1]: import pandas as pd
In [2]: my_file = pd.read_csv('stack.csv')
In [3]: my_file
Out[3]:
anual budget q2 q4
0 100 450 20
1 600 765 50
2 500 380 79
3 800 480 455
4 1100 65 4320
年度预算,第2季度和第4季度总和
my_file['anual budget '].sum()
my_file['q2'].sum()
my_file['q4'].sum()
第三列的平均值
my_file['q4'].mean()
第二列的最小值和最大值
my_file['q2'].max()
my_file['q2'].min()