【问题标题】:Loop through multiple CSV files循环遍历多个 CSV 文件
【发布时间】:2019-07-21 06:34:58
【问题描述】:

我正在使用三个小型数据集,出于可重复性的原因,我正在共享数据here

从第 2 列开始,我想读取当前行并将其与前一行的值进行比较。如果它更大,我会继续比较。如果当前值小于前一行的值,我想将当前值(较小)除以前一个值(较大)。因此,以下代码:

import numpy as np
import matplotlib.pyplot as plt

protocols = {}

types = {"data_c": "data_c.csv", "data_r": "data_r.csv", "data_v": "data_v.csv"}

for protname, fname in types.items():
    col_time,col_window = np.loadtxt(fname,delimiter=',').T
    trailing_window = col_window[:-1] # "past" values at a given index
    leading_window  = col_window[1:]  # "current values at a given index
    decreasing_inds = np.where(leading_window < trailing_window)[0]
    quotient = leading_window[decreasing_inds]/trailing_window[decreasing_inds]
    quotient_times = col_time[decreasing_inds]

    protocols[protname] = {
        "col_time": col_time,
        "col_window": col_window,
        "quotient_times": quotient_times,
        "quotient": quotient,
    }

data_c 是一个numpy.array,它只有一个唯一 quotient0.7data_r 也是一个唯一quotient0.5。但是,data_v 有两个唯一的 quotient 值(0.50.8)。

我想遍历这些 CSV 文件的 quotient 值并使用简单的 if-else 语句(例如,if quotient==0.7: print("data_c"))对它们进行分类,但我收到此错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

更新:我发现这个错误可以通过使用.all()函数来解决,如下所示。

if (quotient==0.7).all():
     print("data_c")
elif (quotient>=0.5).all() and (quotient <=0.8).all():
     print("data_v")
elif (quotient==0.5).all():
     print("data_r")

但是,这会打印出data_c, data_v, data_v。我们该如何解决这个问题?

【问题讨论】:

  • 如何比较第一个元素?你考虑过用熊猫吗?
  • 不,我没有使用pandas
  • 你想看一个例子还是反对?
  • 不,我不反对它,只要它计算我在问题中包含的所有文件的quotient,这就是我坚持使用numpy 的原因之一。
  • 你是否跳过了第一个值?

标签: python arrays numpy csv


【解决方案1】:

如果我理解正确,您要求使用quotient 数组的唯一 值对数据进行分类。如果是这种情况,那么您可以轻松地利用 numpy.unique 提供帮助:

import numpy as np
unique_quotient = np.unique(quotient)
# For data_c this is just a single value

如果您将 unique_quotient 数组放在您的 protocol_dictionary 中,那么它会给您一些比较的东西(例如 using numpy.array_equal):

unique_data_c_quotient = np.r_[ 0.7 ]
if np.array_equal( unique_quotient, unique_data_c_quotient ): 
    print('data_c')
...

【讨论】:

  • 如果商值不固定,假设数据的商在 0.68 和 0.7 (0.68&lt;=quotient&lt;=0.7) 之间怎么办?
  • 我建议您为此提出一个新问题!但是当然有办法
  • 如果您能在这里帮助我,我将不胜感激,但好的,我会这样做的,非常感谢您。批准您的答案为已接受!
【解决方案2】:

我正在尝试复制该过程,直到您获得商数为止。这里我只处理一个文件

import pandas as pd
import numpy as np

# read csv
df = pd.read_csv("/home/baobob/Downloads/data_v.csv", header=None)

# shift down element in column 1
df[2] = df[1].shift()

# this is doing in a vectorial way the comparative with a element 
# with is previous (which is now on the same row but in column 2)
df["quotient"] = np.where(df[1]<df[2], df[1]/df[2], np.nan)

# if we want to check the different quotients for each file 
# we use .unique()
quotients = df["quotient"].unique()

# if we want to remove nan
quotients = [q for q in quotients if not np.isnan(q)]

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 2018-01-15
    • 1970-01-01
    • 2019-04-11
    • 2021-08-26
    相关资源
    最近更新 更多