次のコードは、入力ファイルの各列の平均を計算します。ファイルが平均値を歪める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