Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract the min value and max value from csv file using python

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.

enter image description here

i wrote this code but it seems not working be cause the result is empty .

code:

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))
like image 646
Ghgh Lhlh Avatar asked Sep 03 '25 09:09

Ghgh Lhlh


2 Answers

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.

like image 54
Srinivasan Rajasekaran Avatar answered Sep 04 '25 22:09

Srinivasan Rajasekaran


This might help

import csv

with open('C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv', "r") as csvfile:
    data = csv.reader(csvfile, delimiter=';')
    minVal, maxVal = [], []
    for i in data:
        minVal.append(i[1])
        maxVal.append(i[2])

print min(minVal)
print max(maxVal)
like image 38
Rakesh Avatar answered Sep 04 '25 22:09

Rakesh