代码如下:
import numpy as np import pandas as pd import matplotlib.pyplot as plt data=pd.read_csv('president_heights.csv') heights=np.array(data['height(cm)']) print(heights) #print(data) print("总统身高的平均值是",heights.mean()) print("总统身高的平均值是",np.mean(heights))#这两种方法用来计算平均值都是可以的 print("总统身高的标准差是",heights.std()) print("总统身高的最低值是",heights.min()) print("总统身高的最高值是",heights.max()) print("总统身高的前百分之二十五是",np.percentile(heights,25)) print("总统身高的后百分之二十五是",np.percentile(heights,75)) print("总统身高的中位数是",np.percentile(heights,50)) plt.hist(heights) plt.title("USA presidents' heights") plt.xlabel('heights(cm)') plt.ylabel("People's number") plt.show()
csv文件在github上:https://github.com/jakevdp/PythonDataScienceHandbook/blob/master/notebooks/data/president_heights.csv
输出结果是:
[189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183 177 185 188 188 182 185] 总统身高的平均值是 179.73809523809524 总统身高的平均值是 179.73809523809524 总统身高的标准差是 6.931843442745892 总统身高的最低值是 163 总统身高的最高值是 193 总统身高的前百分之二十五是 174.25 总统身高的后百分之二十五是 183.0 总统身高的中位数是 182.0
图像结果: