python读取csv求平均数_Python-计算CSV文件中每一列的平均值

这篇博客介绍了如何使用Python读取CSV文件,计算每一列的平均值,并展示了一个错误的代码示例及修正后的代码。修正后的代码利用csv模块和Counter对象,处理了无法转换为浮点数的列值,并计算出所有列的平均值。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I'm new in Python and I'm trying to get the average of every (column or row) of a csv file for then select the values that are higher than the double of the average of its column (o row). My file have hundreds of columns, and have float values like these:

845.123,452.234,653.23,...

432.123,213.452.421.532,...

743.234,532,432.423,...

I've tried several changes to my code to get the average for every column (separately), but at the moment my code is like this one:

def AverageColumn (c):

f=open(csv,"r")

average=0

Sum=0

column=len(f)

for i in range(0,column):

for n in i.split(','):

n=float(n)

Sum += n

average = Sum / len(column)

return 'The average is:', average

f.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值