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 7561951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:17:30+00:00 2026-05-30T13:17:30+00:00

EDIT: See end of my post for working code, obtained from zeekay here .

  • 0

EDIT: See end of my post for working code, obtained from zeekay here.

I have a CSV file with two columns (voltage and current). Because the voltage is recorded to many significant digits and the current only has 2, there are many identical current values as the value of the voltage changes. This isn’t important to the programming but I’m just explaining how the data is physically obtained. I want to perform the following action:

For as long as the value of the second column (current) does not change, collect the values of the first column (voltage) into a list and average them. Then write a row into a new CSV file which is this averaged value of the voltage in the first column and the constant current value which did not change in the second column. In other words, if there are 20 rows for which the current did not change (say it is 6 uA), the 20 corresponding voltage values are averaged (say this average comes out to be 600 mV) and a row is generated in a new csv file which reads (‘0.6′,’0.000006’). Then I want to continue iterating through the csv which is being read, repeating the above procedure for each set of fixed currents.

I’ve got the following code so far, but I’m not sure if I’m on the right track:

import sys, csv
with open('filetowriteto.csv','w') as avg:
    loadeddata = open('filetoreadfrom.csv','r')
    writer=csv.writer(avg)
    readloaded=csv.reader(loadeddata)
    listloaded=list(readloaded)
    oldcurrent=listloaded[0][1]
    for row in readloaded:
        newcurrent = row[1]
        biaslist = []
        if newcurrent == oldcurrent:
            biaslist.append(row[0])
        else :
            biasavg = float(sum(biaslist))/len(biaslist)
            writer.writerow([biasavg,newcurrent])
            newcurrent = row[1]

and then I’m not sure where to go.

Edit: It seems that zeekay is on the right track for what I want to do. I’m trying to implement his itertools.groupby() method but I’m currently getting a blank file generated. Here’s my new code so far:

import sys, csv, itertools
with open('VI_avg(12).csv','w') as avg: # this is the file which gets written
    loadeddata = open('VI(12).csv','r') # this is the file which is read
    writer=csv.writer(avg)
    readloaded=csv.reader(loadeddata)
    listloaded=list(readloaded)
    oldcurrent=listloaded[0][1] # looks like this is no longer required
    for current, row in itertools.groupby(readloaded, lambda x: x[1]):
        biaslist = [float(x[0]) for x in row]
        biasavg = float(sum(biaslist))/len(biaslist)
        # write it out
        writer.writerow(biasavg, current)

Suppose the CSV file being opened is something like this (shortened example):

0.595417,0.000065
0.595177,0.000065
0.594937,0.000065
0.594697,0.000065
0.594457,0.000065
0.594217,0.000065
0.593977,0.000065
0.593737,0.000065
0.593497,0.000064
0.593017,0.000064
0.592777,0.000064
0.592537,0.000064
0.592297,0.000064
0.587018,0.000064
0.586778,0.000064
0.586538,0.000063
0.586299,0.000063
0.586059,0.000063
0.585579,0.000063
0.585339,0.000063
0.585099,0.000063
0.584859,0.000063
0.584619,0.000063
0.584379,0.000063
0.584139,0.000063
0.583899,0.000063
0.583659,0.000063

Final update: Here’s the working version, obtained from zeekay:

import csv
import itertools

with open('VI(12).csv') as input, open('VI_avg(12).csv','w') as output:
    reader = csv.reader(input)
    writer = csv.writer(output)
    for current, row in itertools.groupby(reader, lambda x: x[1]):
        biaslist = [float(x[0]) for x in row]
        biasavg = float(sum(biaslist))/len(biaslist)
        writer.writerow([biasavg, current])
  • 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-05-30T13:17:32+00:00Added an answer on May 30, 2026 at 1:17 pm

    You can use itertools.groupby to group results as you read through the csv, which would simplify things a lot. Given your updated example:

    import csv
    import itertools
    
    with open('VI(12).csv') as input, open('VI_avg(12).csv','w') as output:
        reader = csv.reader(input)
        writer = csv.writer(output)
        for current, row in itertools.groupby(reader, lambda x: x[1]):
            biaslist = [float(x[0]) for x in row]
            biasavg = float(sum(biaslist))/len(biaslist)
            writer.writerow([biasavg, current])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See my working code in the answers below. In brief: I have a
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
I have a Controller with two Edit methods (see below). When I submit the
Intro: EDIT: See solution at the bottom of this question (c++) I have a
Edit: I have solved this by myself. See my answer below I have set
I have just successfully installed CakePHP and I see that I can edit the
I have written the following code to perform some simultaneous HTTP posting and file
[Edit] See this post as to why I'm declaring form elements globally. I chose
I am working with a form, and the front end devs have default text
EDIT: I would really like to see some general discussion about the formats, their

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.