Python疑问,求平均值问题

105.408635, 109.058062
109.711955, 107.612010
106.770910, 107.178111
104.902315, 108.942544
106.429432, 109.986264
106.350715, 108.745940
107.208566, 106.724891
105.758081, 107.954024
106.635802, 105.702770
109.028467, 105.940024
104.741757, 109.545083
104.469050, 105.768578
105.860169, 106.943419
104.190133, 108.945727
108.427905, 107.905775
105.105144, 108.668921
105.584954, 110.006551
105.130636, 105.577014
106.174433, 106.636557
107.386285, 108.500267
107.742580, 110.253006
107.594896, 106.710349
109.138761, 106.629520
...... .......
Python读取txt文件,然后咋将第一列数字求平均值啊

4个回答

 import sys

sum = 0
cnt = 0
f = open('1.txt', 'r')
s = f.readline()
while (s):
    sum = sum + float(s.split(",")[0])
    cnt = cnt + 1
    s = f.readline()
print(sum / cnt)
f.close()


106.51093830434785

图片说明

这是运行效果

如果满意,麻烦点个采纳,谢谢。

import pandas as pd

a=pd.read_csv('mean.txt')
mean = a['col1'].mean()
print mean

import sys
sum = 0
cnt = 0
f = open('1.txt', 'r')
files = f.readline()
while (files ):
sum = sum + float(files .split(",")[0])
cnt = cnt + 1
files = f.readline()
print(sum / cnt)
f.close()

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问