如何在Python 3中循环遍历CSV数值数据作为单独的行

huangapple go评论44阅读模式
英文:

How to loop through csv numeric data as individual rows in python 3

问题

I have a CSV file with one excel type column of numeric data in many excel type rows, but I don't understand how to parse through each "row".

例子:我不知道如何将第一行的值-0.164添加到第二行的值-0.1640中。

What I am eventually needing to do is loop through this data and get the average value for the first 10 rows, have if - then statements based on that value. Then get the average value for the next 10 rows and repeat until I'm at the end of the data.

这里是我使用的代码,其中有我尝试理解如何将一行添加到另一行的注释部分

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
        #value = float(row[0]) + float(row[1])
        #print(row[0])
        #print(row[1])
        #print(value)
英文:

I have a CSV file with one excel type column of numeric data in many excel type rows, but I don't understand how to parse through each "row".
Example: I don't know how to add the first row value of -0.164 to the second row value of -0.1640.

What I am eventually needing to do is loop through this data and get the average value for the first 10 rows, have if - then statements based on that value. Then get the average value for the next 10 rows and repeat until I'm at the end of the data.

如何在Python 3中循环遍历CSV数值数据作为单独的行

Here is code I used with commented out lines of me trying to understand how to add one row to another

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
            print(row)
        #value = float(row[0]) + float(row[1])
        #print(row[0])
        #print(row[1])
        #print(value)

答案1

得分: 2

以下是翻译好的部分:

这里的目标是循环遍历文件,同时更新一个变量以跟踪运行总和并输出平均值。你已经解决了循环部分。

float(row[0]) + float(row[1]) 的问题在于,row 变量表示特定行中的所有列值。这意味着它将第一列加到第二列,然后成为变量 value 的值。这不是真正想要的结果。

为了对整个文件进行平均,你可以这样做:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    for row in reader:
        sum_ += float(row[0])
        count += 1
    print("平均值:", sum_ / count)

但你可能不只是想要所有行的平均值,而是想要每组十行的平均值。因此,你需要在循环内部添加一个条件,检查是否已收集足够的值:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    for row in reader:
        sum_ += float(row[0])
        count += 1
        if count >= 10:
            print("平均值:", sum_ / count)
            # 重置平均值
            sum_ = 0
            count = 0
    # 是否有剩余样本?如果是,则打印平均值
    if count > 0:
        print("平均值:", sum_ / count)

这样将为每十个样本打印一次。这对于调试目的很有用,但你可能想将平均值存储在一个变量中,以便可以进行其他处理步骤。

示例:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    samples = []
    for row in reader:
        sum_ += float(row[0])
        count += 1
        if count >= 10:
            samples.append(sum_ / count)
            # 重置平均值
            sum_ = 0
            count = 0
    # 是否有剩余样本?如果是,则添加到列表中
    if count > 0:
        samples.append(sum_ / count)

这将创建一个包含平均数据的列表。

英文:

The goal here is to loop over the file, while updating a variable to keep track of the running sum and output an average. You've already figured out the loop part.

The problem with float(row[0]) + float(row[1]) is that the row variable represents all of the column values within a specific row. So this means that it adds the first column to the second column, and then that becomes the value of the variable value. That's not really what was intended.

What you might do, in order to do an average of the whole file, is this:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    for row in reader:
        sum_ += float(row[0])
        count += 1
    print("Average:", sum_ / count)

But you don't really want an average of all rows, you want an average for each group of ten rows. So you need an if inside the loop, which checks if you've collected enough values:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    for row in reader:
        sum_ += float(row[0])
        count += 1
        if count >= 10:
            print("Average:", sum_ / count)
            # reset average
            sum_ = 0
            count = 0
    # Are there any samples left over? Print an average if so
    if count > 0:
        print("Average:", sum_ / count)

So that will print once for every ten samples. That's useful for debugging purposes, but you probably want to put the averages in a variable, so you can do other processing steps with it.

Example:

import csv
with open('grayramp_line28_NoHblank.csv', newline='') as f:
    reader = csv.reader(f)
    sum_ = 0
    count = 0
    samples = []
    for row in reader:
        sum_ += float(row[0])
        count += 1
        if count >= 10:
            samples.append(sum_ / count)
            # reset average
            sum_ = 0
            count = 0
    # Are there any samples left over? Add to list if so
    if count > 0:
        samples.append(sum_ / count)

This creates a list with the averaged data.

huangapple
  • 本文由 发表于 2023年5月7日 06:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191416.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定