如何找到 csv 文件中一列的平均值?

How to find the average of a column in a csv file?

我正在尝试查找此 csv 文件中一列的平均值。 我设法读取了 csv 文件:

from numpy import genfromtxt

emp_code = genfromtxt('C:\Users\V\Desktop\data science\DS1_C4_S1_Numpy_PracticeBrief.csv', delimiter=',', dtype=None)

emp_code

array([['1', '22'],
       ['2', '27'],
       ['3', '31'],
       ['4', '29'],
       ['5', '27'],
       ['6', '26'],
       ['7', '27'],
       ['8', '23'],
       ['9', '27'],
       ['10', '32'],
       ['11', '28'],
       ['12', '27'],
       ['13', '22'],
       ['14', '27'],
       ['15', '35'],
       ['16', '33'],
       ['17', '28'],
       ['18', '22'],
       ['19', '25'],
       ['20', '27'],
       ['21', '37'],
       ['22', '27'],
       ['23', '24'],
       ['24', '27'],
       ['25', '28'],
       ['26', '26'],
       ['27', '27'],
       ['28', '27'],
       ['29', '29'],
       ['30', '49'],
       ['31', '26'],
       ['32', '32'],
       ['33', '26'],
       ['34', '24'],
       ['35', '24'],
       ['36', '23'],
       ['37', '28'],
       ['38', '28'],
       ['39', '22'],
       ['40', '26'],
       ['41', '32'],
       ['42', '35'],
       ['43', '33'],
       ['44', '31'],
       ['45', '30'],
       ['46', '36'],
       ['47', '22'],
       ['48', '23'],
       ['49', '32'],
       ['50', '34']], dtype='<U2')

现在,我正在尝试找出平均年龄。我不确定如何找到它。是 np.mean['Age'] 吗? 编辑:我试过 emp_code[:,1].mean() 并且它给出了一个错误。 这是一个类型错误,所以我无法理解其背后的原因。

数据类型有问题。您可能需要类型转换(转换)或者从文本文件中读取浮点数作为数据类型。

从文本文件中将其读取为浮点数:

emp_code = genfromtxt('C:\Path\to\csv\DS1_C4_S1_Numpy_PracticeBrief.csv', delimiter=',', dtype=np.float64)
mean_age = emp_code[:,1].mean()

此代码可以帮助您将 numpy 数组转换为浮点数并获取平均年龄。

emp_code = np.asarray(emp_code, dtype=np.float64)
mean_age = emp_code[:,1].mean()