简体   繁体   English

在纯python(没有numpy等)中,如何找到二维列表中某些列的平均值?

[英]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.我目前使用 CSV 阅读器来创建一个二维列表。 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.我可能不关心速度,我在阅读了 csv 之后做了一次,我的 CSV 文件可能有 2000 行左右,只有 30 列左右。

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这是假设所有行的长度相等,如果不是,您可能需要添加一些 try / except case

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.您将列表的名称和列的索引(从 0 开始)传递给它,它将返回这些列的平均值。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python的多维列表中找到最小值,第二至最小值等的索引? - How do I find the index for the minimum, second to minimum, etc. in a multi-dimensional list in Python? 我怎样才能 file.find("a","b","c"..... 等 "z") 和 ord("A","B","C"..... 等。蟒蛇中的“Z”) - How can I file.find("a","b","c"..... etc. "z") and ord("A","B","C"..... etc. "Z") in python 如何用python矩阵描述(均值,中位数,计数等)所有两因子列组合? - How to describe (mean, median, count, etc.) all two-factor column combinations in a matrix with python? 如何聚合(总和、平均值等)值并基于此创建新的 Pandas dataframe? - How can I aggregate (sum, mean, etc.) values and create a new Pandas dataframe based on that? 如何从numpy.datetime64对象中获取小时,分钟等? - How can I get an hour, minute etc. out of numpy.datetime64 object? 如何获取“matplotlib”、“numpy”、“scipy”、“pandas”等的存根文件? - How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.? Python:我如何强制1元素NumPy数组是二维的? - Python: How can I force 1-element NumPy arrays to be two-dimensional? 如何在numpy中找到二维数组的argmax? - How to find the argmax of a two dimensional array in numpy? 如何找到与某个列表匹配的numpy二维数组中的所有元素? - How to find all elements in a numpy 2-dimensional array that match a certain list? 如何创建一个 for 循环以从 Python 中两个数据帧的某些列的 Wilcoxon 测试中创建我想要的 p 值列表? - How can I create a for loop to create a list of the p-values I want from a Wilcoxon Test of certain columns of two dataframes in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM