如何使用python从csv文件中提取最小值和最大值 [英] how to extract the min value and max value from csv file using python
本文介绍了如何使用python从csv文件中提取最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个python脚本,可从csv文件读取并将请求的列附加到2空列表中.之后,我需要提取所提取列的最小值和最大值.
i have a python script that read from csv file and append the requested columns into 2 empty list. after that i need to extract the minimum and maximum value of the columns extracted.
我写了这段代码,但由于结果为空,所以似乎不起作用.
i wrote this code but it seems not working be cause the result is empty .
import csv
mydelimeter = csv.excel()
mydelimeter.delimiter=";"
myfile = open("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv")
myfile.readline()
myreader=csv.reader(myfile,mydelimeter)
mywind,mydate=[],[]
minTemp, maxTemp = [],[]
for row in myreader:
print(row[1],row[2])
minTemp.append(row[1])
maxTemp.append(row[2])
print ("min value element : ", min(minTemp))
print ("max value element : ", min(maxTemp))
推荐答案
您可以使用Pandas,将数据加载到DataFrames中,它们具有诸如Sum,Max,Min,AVG等内置功能.</p>
You can Use Pandas Where You can load the data into DataFrames and They have inbuilt functions such as Sum,Max,Min,AVG etc.
import pandas as pd
df=pd.read_csv('Name.csv')
#FINDING MAX AND MIN
p=df['ColumnName'].max()
q=df['ColumnName'].min()
print(q)
就这样,您将在指定"列中找到最小值".
Thats it You will find the Min value in the Specified column.
这篇关于如何使用python从csv文件中提取最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文