我是新来的在python上读取文件的人。我正在尝试计算.dat文件中每一列的平均值。
我的代码未运行,并且未显示任何错误。
import pandas as pd
with open('airfoil_self_noise.dat') as f:
next(f) # skip first row
df = pd.DataFrame(l.rstrip().split() for l in f)
print(df)
def average_column (df):
f = open(df,"r")
average = 0
Sum = 0
row_count = 0
for row in f:
for column in row.split(','):
n=float(column)
Sum += n
row_count += 1
average = Sum / len(column)
f.close()
return 'The average is:', average