import random #隨機數的包
import pandas as pd
#Python Data Analysis Library 或 pandas 是基於NumPy 的一種工具,該工具是為了解決數據分析任務而創建的。Pandas 納入了大量庫和一些標準的數據模型,提供了高效地操作大型數據集所需的工具。pandas提供了大量能使我們快速便捷地處理數據的函數和方法。你很快就會發現,它是使Python成為強大而高效的數據分析環境的重要因素之一
import matplotib.pypot as plt
#Matplotlib 是一個 Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環境生成出版質量級別的圖形。通過 Matplotlib,開發者可以僅需要幾行代碼,便可以生成繪圖
import numpy as np
#NumPy(Numerical Python) 是 Python 語言的一個擴展程序庫,支持大量的維度數組與矩陣運算,此外也針對數組運算提供大量的數學函數庫。
#顯示漢字
import matplotlib as mpl
mpl.rcParams[『font.sans-serif』]=[『SimHei』] #用來正常顯示中文標籤
mpl.rcParams[『axes.unicode_minus』]=False #用來正常顯示負號
import randomimport pandas as pdimport numpy as npimport matplotlib.pyplot as plt#顯示漢字import matplotlib as mplmpl.rcParams['font.sans-serif']=['SimHei'] #用來正常顯示中文標籤mpl.rcParams['axes.unicode_minus']=False #用來正常顯示負號
def displayinfor2(sex,height,weight): stard=105 if sex=='男': stard=105 else: stard=100 dlta=height-stard per=dlta*0.1 if dlta - per <= weight <= dlta + per: return '標準' elif weight > dlta + per: return '超重' elif weight < dlta - per: return '過輕' else: return '無結果'1234567891011121314151617
append是list類型數據的屬性
要用此屬性應該用以下命令改為list屬性
#list 轉 numpy
np.array(a)
#ndarray 轉 list
a.tolist()
if __name__ == '__main__': colums = ['姓名', '學號', '性別', '身高', '體重'] normarl = ['張三', 100, '男', 175, 45] data = [] # 未來數據 for i in range(20): item1 = normarl[0] + str(i) item2 = normarl[1] + i a = random.randint(0, 1) #隨機數0或1 if a == 0: item3 = normarl[2] else: item3 = '女' item4 = random.randint(165, 200) # 身高 item5 = random.randint(45, 100) # 體重 weight = 0 temp = [] temp.append(item1) temp.append(item2) temp.append(item3) temp.append(item4) temp.append(item5) data.append(temp)
df3 = pd.DataFrame(data, columns=colums) #轉換成excel的格式,data為數據,columns為列數據名稱,index為行數據名稱
sheet_name #表名稱
df3 = pd.DataFrame(data, columns=colums) df3.to_excel('D:/xuesheng.xlsx', sheet_name='學生表')
df4 = pd.read_excel(『D:/xuesheng.xlsx』, index_col=0) #讀取excel表格中的數據,讓第一列成為索引,index_col,讓0名稱成為索引,因為讀取excel會讀取多餘的一列數據,即把索引也讀進來,所以用index_col重新定義一下索引
number = int(input("請輸入一個學號:")) df4 = pd.read_excel('D:/xuesheng.xlsx', index_col=0)
np4 = df4.values #values以列表返回字典中的所有值
temp=i.tolist() #tolist把i的numpy格式轉換成list為了下面append屬性,因為只有list能用append
item6=displayinfor2(i[2],i[3],i[4]) #調用displayinfor2函數,在上面有定義返回值為標準、過輕、過重
append #提交數據的list中。例:temp=[1,『男』,2] temp,append(3) 結果為:temp[1,『男』,2,3]
np4 = df4.values for i in np4: if (i[1] == number): temp=[] temp=i.tolist() item6=displayinfor2(i[2],i[3],i[4]) temp.append(item6) #print(type(temp)) print(temp)
第一張圖(圖片最下面)
學號,身高\體重分布圖
ax.axis([100, 120, 45, 200]) #axis:x軸:100到120,y軸:45到200
ax.plot(data1,data2,label=『身高』) #繪製線 x軸:data1 , y軸:data2,label圖例
ax.legend() #把圖例進行顯示
data1 = [] #學號 data2 = [] #身高 data3 = [] #體重 number=0 # fig, ax = plt.subplots() fig = plt.figure() ax = fig.add_subplot(2, 1, 2) for i in np4: temp1 = i[1] temp2 = i[3] temp3 = i[4] data1.append(temp1) data2.append(temp2) data3.append(temp3) ax.axis([100, 120, 45, 200]) ax.plot(data1, data2,label='身高') ax.plot(data1, data3,label='體重') ax.set_xlabel('x軸(學號)') ax.set_ylabel('y軸(身高\體重)') ax.set_title("學生身高體重圖") ax.legend() # Add a legend.
第二張圖
ax1.bar(names1, heightnum) #柱狀圖 names1表示x軸各個名稱,heightnum表示各個區間的數量
heightnum=[0,0,0,0,0,0,0] #身高區間的人數 for i in data2: if(165<= i <=170): heightnum[0] = heightnum[0] + 1 elif (170<i<=175): heightnum[1] = heightnum[1] + 1 elif (175<i<=180): heightnum[2] = heightnum[2] + 1 elif (180<i<=185): heightnum[3] = heightnum[3] + 1 elif (185 < i <= 190): heightnum[4] = heightnum[4] + 1 elif (190 < i <= 195): heightnum[5] = heightnum[5] + 1 elif (195< i <= 200): heightnum[6] = heightnum[6] + 1 names1=['165~170','171~175','176~180','181~185','186~190','191~195','196~200'] ax1=plt.subplot(221) plt.title("學生身高分布圖") ax1.bar(names1, heightnum)1234567891011121314151617181920
weightnum = [0,0,0,0,0,0,0,0,0,0] # 身高區間的人數 for i in data3: if (45 <= i <= 50): weightnum[0] = weightnum[0] + 1 elif (50 < i <= 55): weightnum[1] = weightnum[1] + 1 elif (55 < i <= 60): weightnum[2] = weightnum[2] + 1 elif (60 < i <= 65): weightnum[3] = weightnum[3] + 1 elif (65 < i <= 70): weightnum[4] = weightnum[4] + 1 elif (70 < i <= 80): weightnum[5] = weightnum[5] + 1 elif (80 < i <= 85): weightnum[6] = weightnum[6] + 1 elif (85 < i <= 90): weightnum[7] = weightnum[7] + 1 elif (90 < i <= 100): weightnum[8] = weightnum[8] + 1 names2 = ['45~50', '51~55', '56~60', '61~65', '66~70', '71~75', '76~80','81~85','86~90','91~100'] plt.subplot(222) plt.title("學生體重分布圖") plt.bar(names2, weightnum) plt.show()
最終結果圖:
此項目完整代碼獲取後臺私信小編01