I have a csv file with 124K medians from different samples. I've created a list from the column of choice (F532 Median) and wanted to get the means of the medians, max number, min number. I was able to get the means, but I'm having problems with the max and min. I've tried sorting the list and get the first element for the min, but I get the same error. ('float' object has no attribute 'sort') I've tried converting to strings, but get the same error ,but with string instead of float. I also tried the non-sort approach using a for loop, but same error. Does anyone have a different approach to solve this? Thank you and any help is much appreciated.
import csv
from pathlib import Path
def process_row(row):
row["F532 Median"] = float(row["F532 Median"])
return row
# File located in home directory
file_path = Path.home()/"python_codes"/"253_Slide01_A1.csv"
with file_path.open(mode="r",encoding="utf-8") as file:
reader = csv.DictReader(file)
row_information = [process_row(row) for row in reader]
t_high = []
count = 0
sum1 = 0
for item in row_information:
t_high = item["F532 Median"]
sum1 = t_high
count = 1
avg = sum1 / count
sort_list = t_high.sort()
print(sort_list[0])
CodePudding user response:
You're replacing the list t_high
with the value of item["F532 Media"]
from each row, so at the end of the loop it will contain the value from the last row. You need to append to the list.
There's no need to recompute the average each time through the loop. Do that at the end. And there's no need to increment count
in the loop, just use len(row_information)
list.sort()
doesn't return the sorted list, it sorts in place. And if you just want the highest, use max(t_high)
.
for item in row_information:
high = item["F532 Median"]
t_high.append(high)
sum1 = high
avg = sum1 / len(row_information)
print(avg, max(t_high))