简体   繁体   中英

In pure python (no numpy, etc.) how can I find the mean of certain columns of a two dimensional list?

I currently use CSV reader to create a two dimensional list. First, I strip off the header information, so my list is purely data. Sadly, a few columns are text (dates, etc) and some are just for checking against other data. What I'd like to do is take certain columns of this data and obtain the mean. Other columns I just need to ignore. What are the different ways that I can do this? I probably don't care about speed, I'm doing this once after I read the csv and my CSV files are maybe 2000 or so rows and only 30 or so columns.

This is assuming that all rows are of equal length, if they're not, you may have to add a few try / except cases in

lst = [] #This is the rows and columns, assuming the rows contain the columns
column = 2 
temp = 0
for row in range (len(lst)):
    temp += lst [row][column]
mean = temp / len (lst)

To test if the element is a number, for most cases, I use

try:
    float(element) # int may also work depending on your data
except ValueError:
    pass

Hope this helps; I can't test this code, as I'm on my phone.

Try this:

def avg_columns(list_name, *column_numbers):
    running_sum = 0
    for col in column_numbers:
        for row in range(len(list_name)):
            running_sum += list_name[row][col]
    return running_sum / (len(list_name)*len(column_numbers))

You pass it the name of the list, and the indexes of the columns (starting at 0), and it will return the average of those columns.

l = [
    [1,2,3],
    [1,2,3]
]
print(avg_columns(l, 0)) # returns 1.0, the avg of the first column (index 0)
print(avg_columns(l, 0, 2)) # returns 2.0, the avg of column indices 0 and 2 (first and third)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM