Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9016609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:02:23+00:00 2026-06-16T04:02:23+00:00

Ok, I have searched for this specific answer after trying to teach myself what

  • 0

Ok, I have searched for this specific answer after trying to teach myself what the best way to do this was. I’m new to python, and was hoping somebody knows a quick way to help me out! Here is the example of the input data file:

Lat,Long,Var,Id,Date Time
47.022,-104.330,10,MBVR,12/12/20 06:36:00
47.022,-104.330,11,MBVR,12/12/20 06:26:00
48.810,-104.253,10,MCOM,12/12/20 06:41:00
48.810,-104.253,13,MCOM,12/12/20 06:38:00
48.810,-104.253,12,MCOM,12/12/20 06:48:00
47.022,-104.330,11,MBVR,12/12/20 05:17:00
47.022,-104.330,10,MBVR,12/12/20 05:34:00
47.022,-104.330,12,MBVR,12/12/20 05:24:00

The file can have many different id’s, and that is just a sample. I have the program down on ingesting the data and separating it out, and writing an output file. Here is a part of my code:

csv_max = 'X:\\csv\\lsrwnd.dat'
my_file = open(csv_max, "rb")
rowadd = my_file.next()
for line in my_file:
    items = line.split(",")
    coords = items[0:2]
    wind = items[2]
    station = items[3]
    timestamp = items[4]

So here’s my problem from this point. What I need to do is create an output file of just the maximum values of the “id” column. So, if “station” is repeated, I need the program to run through each occurrence of that station, find the max “id”, and ONLY return the line of the max value of “id” for each respective “station”. From the data example above, all I want is:

48.810,-104.253,13,MCOM,12/12/20 06:38:00
47.022,-104.330,12,MBVR,12/12/20 05:24:00

and the rest can be dumped. For MCOM, the max id is 13, and for MBVR the max is 12. So if theres 50 different stations, I need only 50 lines returned, which each of the returned 50 would be the max “id” of that station. I can create the output file, but how can I get the max id data for each station, and also the other elements? I tried using dictionaries, but I’m having trouble figuring out how to return the whole line instead of just the maximum value key and station name. The “date time” variable isnt important (in terms of needing the most recent or first occurrence). Thank you in advance for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:02:25+00:00Added an answer on June 16, 2026 at 4:02 am
    import csv
    d = {}
    with open('input.csv', 'r') as f:
        csvr = csv.DictReader(f)
        for rec in csvr:
            if rec['Id'] not in d or int(rec['Var']) > int(d[rec['Id']]['Var']):
                d[rec['Id']] = rec
    with open('output.csv', 'w') as f:
        csvw = csv.DictWriter(f, fieldnames=csvr.fieldnames)
        csvw.writeheader()
        csvw.writerows(d.itervalues())
    

    The output.csv file looks now like this:

    Lat,Long,Var,Id,Date Time
    48.810,-104.253,13,MCOM,12/12/20 06:38:00
    47.022,-104.330,12,MBVR,12/12/20 05:24:00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched SO and couldn't find a specific answer to this problem. I have
I have searched this topic on google a bit and seen some best practices.
I have googled and searched this site, but nothing specific appears and cannot get
I have searched over internet but did not get any specific answer or i
I've searched Google about this specific exception and multiple results have surfaced but I
I have searched on here for some time and can't find a specific answer
I have searched this site and Google and even though the idea is pretty
Preface: I am sure this is incredibly simple, but I have searched this site
I have searched for this, and i found that for api less than 12,
I have searched and searched for this but I think my terminology isn't correct

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.