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 .
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]]
asked Feb 22, 2018 at 5:06
3
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.
DYZ
52.5k10 gold badges61 silver badges88 bronze badges
answered Feb 22, 2018 at 5:15
1
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]
answered Feb 22, 2018 at 5:21
RakeshRakesh
79.3k17 gold badges69 silver badges107 bronze badges
2
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[]]
answered Feb 1 at 14:03
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]]
answered Feb 22, 2018 at 5:56
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]]
answered Feb 22, 2018 at 6:34
Ghgh LhlhGhgh Lhlh
1351 gold badge2 silver badges13 bronze badges