I have a csv file containing 3 columns of data (among others that aren't relevant to this question). The 3 pertinent columns are ID, length, and day. I'm wanting to simply calculate the average length for each ID for day values between 0.1 - 10.9. Each ID has a different amount of day and corresponding length values.
Also, I'm unclear on whether it is better to do this in pandas, or to read in the csv and convert the numeric (day and length) data to floats. Any tips or advice would be appreciated.
My current flow is included below, but it's not running (seems to be stuck in never ending for loop).
df = []
for row in csv.reader(open('/Users/me/Desktop/datafile.csv','r')):
df.append(row)
header= df.pop(0)
#unzip data
ID, day, length = zip(*df)
day_float = []
def _floatday(alist):
for i in range(len(alist)):
day_float.append(float(alist[i]))
return (day_float)
_floatday(day)
length_float = []
def _floatlength(alist):
for i in range(len(alist)):
length_float.append(float(alist[i]))
return (length_float)
_floatlength(length)
avg_initial=0.0
count=0
initial_length =[]
for ID_val in range(len(ID)):
for i in range(len(day_float)):
for p in range(len(length_float)):
if day_float[i] < 10.9:
avg_initial += p
count += 1
average_length = avg_initial/count
initial_length.append(average_length)
return (initial_length)
I realize the functions to convert to floats are super clunky... I'm pretty new to python still. Any help or tips would be appreciated. Thank you.