[英]How to skip a column when reading a CSV file - Python
So in my CSV i have peoples names as the first row and i'm trying to get the average of 3 numbers on the columns 1-3. 因此,在我的CSV中,我将人的姓名作为第一行,并且我尝试获取1-3列中3个数字的平均值。 (not the first column though) Is there a way to skip a column so that i can just pull out columns 1-3?
(虽然不是第一列)有没有一种方法可以跳过一列,这样我就可以拉出1-3列? here is my code for getting the average.
这是我获取平均值的代码。 Any help would be much appreciated.
任何帮助将非常感激。 So just to be clear on what i want: I want to skip a column so that i can successfully get the mean average from columns 1-3.
因此,要明确我想要的是什么:我想跳过一列,以便我可以成功地从1-3列中获得平均值。
if order ==("average score"):
with open("data.csv") as f:
reader = csv.reader(f)
columns = f.readline().strip().split(" ")
numRows = 0
sums = [1] * len(columns)
for line in f:
# Skip empty lines
if not line.strip():
continue
values = line.split(" ")
for i in range(len(values)):
sums[i] += int(values[i])
numRows += 1
for index, summedRowValue in enumerate (str(sums)):
print (columns[index], 1.0 * summedRowValue / numRows)
您需要做的就是将范围更改为从1开始而不是0,以跳过第一列:
for i in range(1, len(values)):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.