【问题标题】:In pure python (no numpy, etc.) how can I find the mean of certain columns of a two dimensional list?在纯 python(没有 numpy 等)中,如何找到二维列表中某些列的平均值?
【发布时间】:2015-12-01 20:50:11
【问题描述】:

我目前使用 CSV 阅读器来创建二维列表。首先,我去掉了标题信息,所以我的列表是纯粹的数据。可悲的是,有几列是文本(日期等),有些只是用于检查其他数据。我想做的是获取这些数据的某些列并获得平均值。我只需要忽略其他列。我有哪些不同的方法可以做到这一点?我可能不在乎速度,我在阅读 csv 后会这样做一次,我的 CSV 文件可能有 2000 行左右,只有 30 列左右。

【问题讨论】:

  • 是因为你没有安装numpy吗?这在 numpy 中是微不足道的。
  • 如果你担心容易安装 numpy,我强烈推荐Anaconda。它是免费的,可以安装 numpy 和许多其他有用的库,无需用户做出很多决定,易于卸载,并且具有许可 license

标签: python arrays csv


【解决方案1】:

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

为了测试元素是否为数字,在大多数情况下,我使用

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

希望这会有所帮助;我无法测试这段代码,因为我在手机上。

【讨论】:

    【解决方案2】:

    试试这个:

    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))
    

    您将列表的名称和列的索引(从 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多