Ghgh Lhlh
Ghgh Lhlh

Reputation: 165

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))

Upvotes: 4

Views: 58023

Answers (5)

Engr. Nnoli K. Pal
Engr. Nnoli K. Pal

Reputation: 11

Just in case you want the max and min values in the entire CSV file. This is applicable to even large datasets. Assume that your file is saved as file.csv

    import pandas as pd 
    dff=pd.read_csv('file.csv')

Since you do not want to involve the dates

    keep_col= ['temperaturemin','temperaturemax'] 

I am using your csv but named it file

    df=dff[keep_col] 

To find specific MAX AND MIN for each column

    a=df['temperaturemin'].max()
    b=df['temperaturemax'].min()
    print(a,"\n",b)

To find MAX AND MIN for the entire file.csv across all columns

    print("Min:",df.min().min(),"Max:",df.max().max())

Upvotes: 1

Rakesh
Rakesh

Reputation: 82785

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)

Upvotes: 2

Ghgh Lhlh
Ghgh Lhlh

Reputation: 165

i found the solution for my question. it seems that the csv file was containing an empty row what i did is i handle the exception in my code so the code becomes:

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])
    try:
        minTemp.append(float(row[1]))
        maxTemp.append(float(row[2]))
    except ValueError:
        print ("error","on line",row)   

print ("min value element : ", min(minTemp))
print ("max value element : ", max(maxTemp)) 

Upvotes: 0

Ashwani Shakya
Ashwani Shakya

Reputation: 439

You can also use numpy library to find min value and max value.

import numpy as np

my_data = np.genfromtxt("C:/Users/test/Documents/R_projects/homework/rdu-weather-history.csv", delimiter=",", skip_header=True)
print ("min value element : ", my_data.min(axis=0)[1])
print ("max value element : ", my_data.max(axis=0)[2])

Upvotes: 0

Srinivasan Rajasekaran
Srinivasan Rajasekaran

Reputation: 585

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.

Upvotes: 16

Related Questions