【问题标题】:check csv every 5 rows with condition using python3.x使用 python3.x 每 5 行检查一次 csv
【发布时间】:2018-01-18 01:48:00
【问题描述】:

csv 数据:

>c1,v1,c2,v2,Time

>13.9,412.1,29.7,177.2,14:42:01

>13.9,412.1,29.7,177.2,14:42:02

>13.9,412.1,29.7,177.2,14:42:03

>13.9,412.1,29.7,177.2,14:42:04

>13.9,412.1,29.7,177.2,14:42:05

>0.1,415.1,1.3,-0.9,14:42:06

>0.1,408.5,1.2,-0.9,14:42:07

>13.9,412.1,29.7,177.2,14:42:08

>0.1,413.4,1.3,-0.9,14:42:09

>0.1,413.8,1.3,-0.9,14:42:10

我目前拥有的代码:

import pandas as pd
import csv 
import datetime as dt


#Read .csv file, get timestamp and split it into date and time separately
Data = pd.read_csv('filedata.csv', parse_dates=['Time_Stamp'], infer_datetime_format=True)
Data['Date'] = Data.Time_Stamp.dt.date
Data['Time'] = Data.Time_Stamp.dt.time
#print (Data)
print (Data['Time_Stamp'])
Data['Time_Stamp'] = pd.to_datetime(Data['Time_Stamp'])
#Read timestamp within a certain range
mask = (Data['Time_Stamp'] > '2017-06-12 10:48:00') & (Data['Time_Stamp']<= '2017-06-12 11:48:00')
june13 = Data.loc[mask]
#print (june13)

我要做的是每5秒读取一次数据,如果c1的5秒数据中有1秒是10.0及以上,则将c1的值替换为0。

我还是 python 的新手,我找不到这方面的例子。我可以得到一些帮助,因为这个问题目前超出了我的 python 编程技能。谢谢!

【问题讨论】:

  • 只是一个旁注,在函数和参数之间插入空格被认为是不好的做法,例如print (Data)。我认为您可以在 Pep8 中找到相关内容。 (python.org/dev/peps/pep-0008/…)
  • 我将摆脱空间并检查链接。谢谢你告诉我
  • 还花了一些时间来尝试回答,但我可能完全不了解您正在尝试做的事情,如果是这样,请说出来,如果您给出一些精确的信息,我会如果我能找到其他解决方案,请编辑我的答案。

标签: python python-3.x pandas csv time


【解决方案1】:

我不知道 csv 文件周围的模块,所以我的回答可能看起来很原始,而且我不太确定您在这里要完成什么,但是您是否考虑过以文本方式处理文件?

据我所知,你想读取每个 c1,检查值并修改它。

要读取和修改文件,您可以:

with open('filedata.csv', 'r+') as csv_file:
    lines = csv_file.readlines()

    # for each line, isolate data part and check - and modify, the first one if needed.
    # I'm seriously not sure, you might have wanted to read only one out of five lines. 
    # For that, just do a while loop with an index, which increments through lines by 5.
    for line in lines:
        line = line.split(',')  # split comma-separated-values

        # Check condition and apply needed change.
        if float(line[0]) >= 10:
            line[0] = "0"  # Directly as a string. 

        # Transform the list back into a single string.
        ",".join(line)

    # Rewrite the file.
    csv_file.seek(0)
    csv_file.writelines(lines)

    # Here you are ready to use the file just like you were already doing.
    # Of course, the above code could be put in a function for known advantages.

(我这里没有python,所以无法测试,可能有错别字。)

如果您只需要数据框而不修改文件:

说实话几乎一样。 而不是最后的文件写入,你可以这样做:

from io import StringIO  # pandas needs stringIO instead of strings.

# Above code here, but without the last 6 lines.

Data = pd.read_csv(
    StringIo("\n".join(lines)),
    parse_dates=['Time_Stamp'],
    infer_datetime_format=True
)

这应该为您提供您拥有的数据,并在需要时更改值。

希望这不是完全关闭。此外,有些人可能会觉得这种方法很可怕;我们已经编写了工作模块来做这类事情,那么为什么要自己处理粗糙的原始数据呢?就个人而言,我认为如果我不尝试了解如何使用文件的文本表示,这通常比学习我将在生活中使用的所有外部模块容易得多。你的意见可能不同。

此外,此代码可能会导致性能下降,因为我们需要对文本进行两次迭代(熊猫在阅读时会这样做)。但是,我认为您不会像已经做的那样通过读取 csv 来获得更快的结果,然后遍历数据以检查条件。 (您可能会赢得每个 c1 检查值的强制转换,但差异很小,并且遍历 pandas 数据帧可能也比列表慢,具体取决于它们当前优化的状态。)

当然,如果你真的不需要 pandas 数据帧格式,你可以完全手动完成,它只需要多几行(或者不需要,tbh)并且不应该更慢,因为迭代次数将最小化:您可以在读取数据的同时检查数据条件。时间不早了,我相信你可以自己弄清楚,所以我不会在我的优秀编辑器(称为 stackoverflow)中编写代码,问有没有什么!

【讨论】:

  • 确实需要读取c1的每一行,但我真正想做的是一次读取5行,检查c1是否只有1个值更多在这 5 行中大于 10.0,如果满足条件,则将 c1( > 10.0 ) 的值替换为 0.0。也感谢您分享您的答案,但我认为这不是我想要得到的。
  • 我首先从实现你在我脑海中所说的开始,然后我意识到了。如果您要更改的值大于 10.0,那么读取 5 与逐一读取以检查相同的条件有何不同? ---- 无论如何,如果你真的想这样做,你仍然可以使用index 变量遍历五个元素列表,它的结尾等效(索引+ 5)(小心,可能会出现超出范围的索引)并使用切片...
猜你喜欢
  • 2013-04-24
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2020-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多