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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:31:49+00:00 2026-05-16T22:31:49+00:00

Ok I have the following working program. It opens of a file of data

  • 0

Ok I have the following working program. It opens of a file of data in columns that is too large for excel and finds the average value for each column:

Sample data is:

Joe Sam Bob
1   2   3
2   1   3

And it returns

Joe Sam Bob
1.5 1.5 3

This is good. The problem is some columns have NA as a value. I want to skip this NA and calculate the average of the remaining values
So

Bobby
1
NA
2

Should output as

Bobby
1.5

Here is my existing program built with help from here. Any help is appreciated!

with open('C://avy.txt', "rtU") as f:
    columns = f.readline().strip().split(" ")
    numRows = 0
    sums = [0] * len(columns)

    for line in f:
        # Skip empty lines
        if not line.strip():
            continue

        values = line.split(" ")
        for i in xrange(len(values)):
            sums[i] += int(values[i])
        numRows += 1

        with open('c://finished.txt', 'w') as ouf:
             for index, summedRowValue in enumerate(sums):
                 print>>ouf, columns[index], 1.0 * summedRowValue / numRows

Now I have this:

with open(‘C://avy.txt’, “rtU”) as f:

def get_averages(f):
   headers = f.readline().split()
   ncols = len(headers)
   sumx0 = [0] * ncols
   sumx1 = [0.0] * ncols
   lino = 1

for line in f:
   lino += 1
   values = line.split()

for colindex, x in enumerate(values):
        if colindex >= ncols:
             print >> sys.stderr, "Extra data %r in row %d, column %d" %(x, lino, colindex+1)
             continue
             try:
                value = float(x)
             except ValueError:
               continue
               sumx0[colindex] += 1
        sumx1[colindex] += value
        print headers
print sumx1
print sumx0
averages = [
    total / count if count else None
   for total, count in zip(sumx1, sumx0)
    ]
print averages

and it says:

Traceback (most recent call last):
File “C:/avy10.py”, line 11, in
lino += 1
NameError: name ‘lino’ is not defined

  • 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-16T22:31:50+00:00Added an answer on May 16, 2026 at 10:31 pm

    The following code handles varying counts properly, and also detects extra data … in other words, it’s rather robust. It could be improved by explicit messages (1) if the file is empty (2) if the header line is empty. Another possibility is testing explicitly for "NA", and issuing an error message if a field is neither "NA" nor floatable.

    >>> import sys, StringIO
    >>>
    >>> data = """\
    ... Jim Joe Billy Bob
    ... 1   2   3     x
    ... 2   x   x     x  666
    ...
    ... 3   4   5     x
    ... """
    >>>
    >>> def get_averages(f):
    ...     headers = f.readline().split()
    ...     ncols = len(headers)
    ...     sumx0 = [0] * ncols
    ...     sumx1 = [0.0] * ncols
    ...     lino = 1
    ...     for line in f:
    ...         lino += 1
    ...         values = line.split()
    ...         for colindex, x in enumerate(values):
    ...             if colindex >= ncols:
    ...                 print >> sys.stderr, "Extra data %r in row %d, column %d" %
    (x, lino, colindex+1)
    ...                 continue
    ...             try:
    ...                 value = float(x)
    ...             except ValueError:
    ...                 continue
    ...             sumx0[colindex] += 1
    ...             sumx1[colindex] += value
    ...     print headers
    ...     print sumx1
    ...     print sumx0
    ...     averages = [
    ...         total / count if count else None
    ...         for total, count in zip(sumx1, sumx0)
    ...         ]
    ...     print averages
    

    Edit add here:

    ...     return headers, averages
    
    ...
    >>> sio = StringIO.StringIO(data)
    >>> get_averages(sio)
    Extra data '666' in row 3, column 5
    ['Jim', 'Joe', 'Billy', 'Bob']
    [6.0, 6.0, 8.0, 0.0]
    [3, 2, 2, 0]
    [2.0, 3.0, 4.0, None]
    >>>
    

    Edit

    Normal usage:

    with open('myfile.text') as mf:
       hdrs, avgs = get_averages(mf)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following program Extract.pl, which opens a file, finds the lines containing
I am working with NSCache in iOS i have following code in .h file
I have the following: An environment that is working like a hash for rows
I am working on a .NET program that starts a new instance of Excel,
I am working on a C program that uses sockets to retrieve a file
I'm working with Python 2.7 on Windows 8/XP. I have a program A that
I am working on a Java program that reads a text file line-by-line, each
I have the following working tar -pcvf base.tar input/myPacket/my2 --exclude-vcs input/myPacket/my3/*.bmp When i have
In YUI I have following code working for mouse wheel. How do I make
We have the following code working for a complex rails form with checkboxes. I'm

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.