如何在某些数据中调用行?

我有一些看起来像this的数据。

我需要调用第一列和第二列(时间和毫升)。

我已导入所有相关内容。

这是我要处理的代码部分:

 time = []
 d180= []

 a = np.arange(0.0,5320.0,1.0)
 b = np.array(a,dtype=float)


 file = open(filename)

 for line in file:
  for i in a:
     if line[0] == 'P':
         False
     elif line[0] == 'T':
         False
     else:

         fields = (line.strip()).split('\t')
         time.append(fields[0])
         d180.append(fields[1])


file.close()     


time = np.array(time,dtype=float)
d180 = np.array(benthic,dtype=float)

plt.figure()
plt.plot(time,d180)
plt.xlabel('Time')
plt.ylabel('ml')
plt.show()

现在,我收到d180.append(fields [1])的索引错误“列表超出范围”。

我应该提到numpy数组b是以前尝试这种方法时遗留下来的:

for i in len(b):
  for line in file:
      if float(line[0]) == b[i]
          fields = (line.strip()).split('\t')
          time.append(fields[0])
          d180.append(fields[1])

但这可能不可行,原因可能有几个,主要是因为无法将“ P”或“ T”转换为浮点数。

最后,数据文件是.txt文件可能与之相关。

LT198986 回答:如何在某些数据中调用行?

我建议使用read_table from Pandas,您将执行以下操作:

import pandas as pd

df = pd.read_table(
  filename,skiprows=6,names=['time','d180','stderr'],)

plt.plot(df['time'],df['d180'])
,

我想我刚刚看到它:您在问题中未能发布的输入有一个空白行。使用以下代码处理空白时:

     fields = (line.strip()).split('\t')
     time.append(fields[0])
     d180.append(fields[1])

fields将具有0或1个元素,具体取决于“空白”中的实际字符。

本文链接:https://www.f2er.com/3122046.html

大家都在问