当我尝试计算列的平均值时,我一直在“无法使用灵活类型执行缩减”,这是我的智慧结束,文件被正确读取(任何行/列中没有缺失值)但是当我加入时: Brain_wt_mean = np.mean(ifile axis = 0)然后Python 2.7.5不喜欢它。我在Spyder IDE中使用它。非常感谢您的帮助。
import os
import numpy as np
if __name__ == "__main__":
try:
curr_dir = os.getcwd()
file_path = curr_dir + '\\brainandbody.csv'
ifile = np.loadtxt('brainandbody.csv', delimiter=',', skiprows=1, dtype=[('brainwt', 'f8'), ('bodywt', 'f8')])
except IOError:
print "The file does not exist, exiting gracefully"
Brain_wt_mean = np.mean(ifile axis=0)
### BELOW is a sample of the csv file ######
Brain Weight Body Weight
3.385 44.5
0.48 15.5
1.35 8.1
465 423
36.33 119.5
27.66 115
14.83 98.2
1.04 5.5
答案 0 :(得分:5)
当你使用像这样的结构化数组时,你会失去一些灵活性。不过,您可以在选择合适的作品后采用均值:
>>> ifile
array([(3.385, 44.5), (0.48, 15.5), (1.35, 8.1), (465.0, 423.0),
(36.33, 119.5), (27.66, 115.0), (14.83, 98.2), (1.04, 5.5)],
dtype=[('brainwt', '<f8'), ('bodywt', '<f8')])
>>> ifile["brainwt"].mean()
68.759375000000006
>>> ifile["bodywt"].mean()
103.66249999999999
我几乎每天都使用numpy
,但在使用我要命名列的排序数据时,我认为pandas
库使事情变得更加方便,而且它可以很好地互操作。值得一看。例如:
>>> import pandas as pd
>>> df = pd.read_csv("brainandbody.csv", skipinitialspace=True)
>>> df
Brain Weight Body Weight
0 3.385 44.5
1 0.480 15.5
2 1.350 8.1
3 465.000 423.0
4 36.330 119.5
5 27.660 115.0
6 14.830 98.2
7 1.040 5.5
>>> df.mean()
Brain Weight 68.759375
Body Weight 103.662500
dtype: float64