下一個代碼計算輸入文件中每列的平均值。它一直運行,直到文件有nan
的值扭曲平均值。Python - 在計算之前從文件中過濾行
這裏是我的代碼:
with open(biasfile, 'r') as f:
data = [map(float, line.split()) for line in f]
num_rows = len(data)
num_cols = len(data[0])
totals = num_cols * [0.0]
for line in data:
for index in xrange(num_cols):
totals[index] += line[index]
averages = [total/num_rows for total in totals]
print averages
這是文件的一部分:
22.7061 5.4303
32.2040 5.4364
22.9982 5.4426
nan 5.4487
nan 5.4548
nan 5.4610
這是輸出:
[nan, 3.1446607421875]
我想忽略nan
值和計算其餘值的平均值。我怎麼能這樣做?
你應該定義簽出[pandas](https://pandas.pydata.org/pandas-docs/stable/index.html)和[numpy](https://docs.scipy.org/doc/numpy/ index.html) – Quickbeam2k1