Re: find max and min values from a column of a csv file

Discussion in 'Python' started by John [H2O], Jul 7, 2011.

  1. John [H2O]

    John [H2O] Guest

    I think you would benefit from reading the data into a numpy array first,
    then using numpy min, max functions.



    prakash jp wrote:
    >
    > Hi All ,
    >
    > Could any one help to get max and min values from a specified column of a
    > csv file. The* csv file is large* and hence the below code did go bad.
    > *Alternate
    > methods would be highly appreciated
    > **
    > minimum.py*:
    > import csv
    > filename = "testLog_4.csv"
    > f = open(filename)
    > def col_min(mincname):
    > with open(filename, 'rb') as f:
    > reader = csv.reader(f, delimiter=",")
    > #print reader
    > #print mincname
    > col_index = next(reader).index(mincname)
    > #print col_index
    > least = min(rec[col_index] for rec in reader)
    > print least
    >
    > col_min(str("Server Latency"))
    > col_min(str("Client Latency"))
    > f.close()
    >
    > *maximum.py:*
    > import csv
    > filename = "testLog_4.csv"
    > f = open(filename)
    > def col_max(maxcname):
    > with open(filename, 'rb') as f:
    > reader = csv.reader(f, delimiter=",")
    > #print reader
    > #print cname
    > col_index = next(reader).index(maxcname)
    > #print col_index
    > highest = max(rec[col_index] for rec in reader)
    > print highest
    >
    > col_max(str("Server Latency"))
    > col_max(str("Client Latency"))
    > f.close()
    >
    >
    > Thanks
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    >


    --
    View this message in context: http://old.nabble.com/find-max-and-min-values-from-a-column-of-a-csv-file-tp32011861p32012126.html
    Sent from the Python - python-list mailing list archive at Nabble.com.
     
    John [H2O], Jul 7, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lois
    Replies:
    1
    Views:
    3,407
    Ryan Stewart
    Dec 27, 2004
  2. Vijai Kalyan
    Replies:
    1
    Views:
    324
    Howard Hinnant
    Mar 21, 2006
  3. Gary Wessle

    min and max running values

    Gary Wessle, Mar 5, 2007, in forum: C++
    Replies:
    41
    Views:
    1,088
    Pete Becker
    Mar 7, 2007
  4. Mithil

    Max and min values in DTD

    Mithil, Aug 27, 2007, in forum: XML
    Replies:
    5
    Views:
    1,029
    Joseph Kesselman
    Aug 28, 2007
  5. Dennis Lee Bieber
    Replies:
    0
    Views:
    628
    Dennis Lee Bieber
    Jul 7, 2011
Loading...

Share This Page