How to find max value in csv 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.

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

Not the answer you're looking for? Browse other questions tagged python list csv max min or ask your own question.

How to find max value in python DataFrame?

Pandas dataframe. max[] method finds the maximum of the values in the object and returns it. If the input is a series, the method will return a scalar which will be the maximum of the values in the series.

How to find max value in a column in python?

To find the maximum value of a column and to return its corresponding row values in Pandas, we can use df. loc[df[col]. idxmax[]].

How do you find the mean of a csv file in Python?

Steps to Calculate Stats from an Imported CSV File.
Step 1: Copy the Dataset into a CSV file. To begin, you'll need to copy the above dataset into a CSV file. ... .
Step 2: Import the CSV File into Python. ... .
Step 3: Use Pandas to Calculate Stats from an Imported CSV File..

How do I extract data from a CSV file in Python?

2.1 Using csv.reader.
Import the csv library. import csv..
Open the CSV file. The .open[] method in python is used to open files and return a file object. ... .
Use the csv.reader object to read the CSV file. csvreader = csv.reader[file].
Extract the field names. ... .
Extract the rows/records. ... .
Close the file..

Chủ Đề