嘿,我需要盡快知道如何從整個檔案中的檔案 .csv 中獲取最大值,而我只是 python 的初學者,我昨天學到了我搜索了很多十個我找到了這段代碼,但它對我沒有用:
import csv #librairies
import math
with open('Classeur8.csv') as csvfile:
reader=csv.reader(csvfile,delimiter=";")
linecount=0
id_max=0
temp_max= -math.inf
for row in reader:
if linecount==0:
print(f'column names are {",".join(row)}')
linecount =1
else:
print(f'our id is {row[0]} and temperature is {row[1]} ')
linecount =1
if float(row[1]) > temp_max:
id_max,temp_max=row[0],float(row[1])
print(f'processed lines:{linecount}')
print(f'max_id:{id_max} and max_temperature:{temp_max}')
Classeur8.csv
id;temperature
2;23
3;33
4;43
5;53
6;63
uj5u.com熱心網友回復:
您可以使用 Pandas:它具有內置功能,例如Sum、Max、Min、AVG等。
import pandas as pd
df=pd.read_csv('Classeur8.csv', sep=';')
#FINDING MAX
p=df['temperature'].max()
print(p)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/493612.html
上一篇:從網路服務器下載我的程式資料(它基本上只是一個.exe變成了.txt)但是當我把它放入一個.exe時它不運行?
下一篇:如何在c語言中跳過檔案的前兩行?
