我的问题:我有一个csv
文件,其数据深度在90到3米之间,并且数据会像这样来回移动。我正在使用最新的python。如何通过python中的csv文件中的范围进行排序?
ex。 (深度)88,77,50,20,5,90,76,54,34,15,8,4,81,74,62,51,49,30,22,10,8 ...等等。它保持从90到3,然后再回来。
我想要做的是每次它在90和3之间分开数据。一旦它分开,我想要在列表中的最后和第一个值。 像这样 ex。 (此处分开),90,76,54,34,15,8,4(此处分离)81,74,62,51,49,30,22,10,8在此分开)... 等等。它保持从90到3,然后再回来。
我该如何去做这件事?或者我会如何将他们分成列表,然后使用他们每个人的数据?
下面的代码我到目前为止:
import csv, numpy
from collections import defaultdict
columns = defaultdict(list) # each value in each column is appended to a list
with open('C:\\Users\\AdamStoer\\Documents\\practicedata.csv') as f:
reader = csv.DictReader(f,delimiter=',') # read rows into a dictionary format
for row in reader:
r = float(row['roll'])
p = float(row['pitch'])
if 0.21 <= p <= 0.31:
if -0.06 <= r <= 0.06:
columns['pitch'].append(row['pitch'])
columns['roll'].append(row['roll'])
columns['i_depth'].append(row['i_depth'])
columns['irrad2'].append(row['sci_ocr504i_irrad2'])
print ('Pitch:')
print (columns['pitch'])
print ('Roll:')
print (columns['roll'])
print ('Depth')
print (columns['i_depth'])
print ("Irrandiance(2):")
print (columns['irrad2'])
irradlst = columns['irrad2']
irradfirst = irradlst[0]
irradlast = irradlst[-1]
depthlst = columns['i_depth']
depthfirst = depthlst[0]
depthlast = depthlst[-1]
print ("\nDepth 1 is " + depthfirst + " and " + "Depth 2 is " + depthlast)
print ("\nIrradiance 1 is " + irradfirst + " and " + "Irradiance 2 is " + irradlast)
#Find the Volume Attenuation Coefficient
#irranddiv = deepest/shallowest
irraddiv = float(irradfirst)/float(irradlast)
#depthdif = deepest-shallowest
depthdif = float(depthfirst) - float(depthlast)
#Find Log of irraddiv
irradlog = numpy.log(irraddiv)
#Find K
K = irradlog/(-depthdif)
print("\nAttenuation Coefficient")
print (K)
谢谢你的回答! – Adam
我想知道你能否解释一下你的代码? – Adam
@Adam我加了一些代码说明 – ali