【python数据分析】亚太地区的商学院(商务与经济统计案例3-3)数据分析
數據分析學了幾個星期了,中間一直在學習模仿別人的代碼,所以沒什么原創可寫。今天看《商務與經濟統計》,想拿個案例來練練手。案例數據也不大:
| Business School | Full-Time Enrollment | Students per Faculty | Local Tuition ($) | Foreign Tuitiion ($) | Age | %Foreign | GMAT | English Test | Work Experience | Starting Salary ($) |
| Melbourne Business School | 200 | 5 | 24,420 | 29,600 | 28 | 47 | Yes | No | Yes | 71,400 |
| University of New South Wales (Sydney) | 228 | 4 | 19,993 | 32,582 | 29 | 28 | Yes | No | Yes | 65,200 |
| Indian Institute of Management (Ahmedabad) | 392 | 5 | 4,300 | 4,300 | 22 | 0 | No | No | No | 7,100 |
| Chinese University of Hong Kong | 90 | 5 | 11,140 | 11,140 | 29 | 10 | Yes | No | No | 31,000 |
| International University of Japan (Niigata) | 126 | 4 | 33,060 | 33,060 | 28 | 60 | Yes | Yes | No | 87,000 |
| Asian Institute of Management (Manila) | 389 | 5 | 7,562 | 9,000 | 25 | 50 | Yes | No | Yes | 22,800 |
| Indian Institute of Management (Bangalore) | 380 | 5 | 3,935 | 16,000 | 23 | 1 | Yes | No | No | 7,500 |
| National University of Singapore | 147 | 6 | 6,146 | 7,170 | 29 | 51 | Yes | Yes | Yes | 43,300 |
| Indian Institute of Management (Calcutta) | 463 | 8 | 2,880 | 16,000 | 23 | 0 | No | No | No | 7,400 |
| Australian National University (Canberra) | 42 | 2 | 20,300 | 20,300 | 30 | 80 | Yes | Yes | Yes | 46,600 |
| Nanyang Technological University (Singapore) | 50 | 5 | 8,500 | 8,500 | 32 | 20 | Yes | No | Yes | 49,300 |
| University of Queensland (Brisbane) | 138 | 17 | 16,000 | 22,800 | 32 | 26 | No | No | Yes | 49,600 |
| Hong Kong University of Science and Technology | 60 | 2 | 11,513 | 11,513 | 26 | 37 | Yes | No | Yes | 34,000 |
| Macquarie Graduate School of Management (Sydney) | 12 | 8 | 17,172 | 19,778 | 34 | 27 | No | No | Yes | 60,100 |
| Chulalongkorn University (Bangkok) | 200 | 7 | 17,355 | 17,355 | 25 | 6 | Yes | No | Yes | 17,600 |
| Monash Mt. Eliza Business School (Melbourne) | 350 | 13 | 16,200 | 22,500 | 30 | 30 | Yes | Yes | Yes | 52,500 |
| Asian Institute of Management (Bangkok) | 300 | 10 | 18,200 | 18,200 | 29 | 90 | No | Yes | Yes | 25,000 |
| University of Adelaide | 20 | 19 | 16,426 | 23,100 | 30 | 10 | No | No | Yes | 66,000 |
| Massey University (Palmerston North, New Zealand) | 30 | 15 | 13,106 | 21,625 | 37 | 35 | No | Yes | Yes | 41,400 |
| Royal Melbourne Institute of Technology? | 30 | 7 | 13,880 | 17,765 | 32 | 30 | No | Yes | Yes | 48,900 |
| Jamnalal Bajaj Institute of Management Studies (Bombay) | 240 | 9 | 1,000 | 1,000 | 24 | 0 | No | No | Yes | 7,000 |
| Curtin Institute of Technology (Perth) | 98 | 15 | 9,475 | 19,097 | 29 | 43 | Yes | No | Yes | 55,000 |
| Lahore University of Management Sciences | 70 | 14 | 11,250 | 26,300 | 23 | 2.5 | No | No | No | 7,500 |
| Universiti Sains Malaysia (Penang) | 30 | 5 | 2,260 | 2,260 | 32 | 15 | No | Yes | Yes | 16,000 |
| De La Salle University (Manila) | 44 | 17 | 3,300 | 3,600 | 28 | 3.5 | Yes | No | Yes | 13,100 |
?2.導入數據
import pandas as pd import numpy as np data = pd.read_csv('H:/商務與經濟統計(原書第12版)/數據文件/第3章/Asian.CSV') data.head()3.對數據集中的每一個變量進行匯總,根據最大值,最小值等進行評價和解釋
data.describe()?
可以知道一共有25個business school,平均錄取名額為165人,最多的學校錄取463人,最少12人,差別還是挺大的。每個學院平均錄取8人,最多19人最少2人,差別還行,不是太大,50%以上的學校都錄取7人。平均年齡28歲,最大37歲,最小22歲。平均錄取外國人的比例28%,最大達到90%,最少的為0,干脆不招外國人。新見解?感覺年齡都比較大,不同學校錄取人數差別大,但是平均每個學院人數又差不太多,估計是有的學校學院比較多吧。有的學校外國學生比例很大,有的根本沒有,差別還是挺大的,不知道是不是排外或者在外地知名度不高。
這里學費和起薪都沒有算作數量變量,可見后面要進行處理。
4.本國學生和外國學生學費的差別
這里發現列名寫錯了先用rename進行更正。
然后把金額里的逗號給去掉,并改成float型。
抽取本國學生和外國學生學費的列,plot畫圖。
#本國學生和外國學生學費的差別#列名寫錯了,先改掉 data = data.rename(columns={'Foreign Tuitiion ($)':'Foreign Tuition ($)'}) #把數字的逗號去掉 data['Local Tuition ($)'] = data['Local Tuition ($)'].apply(lambda x:x.replace(',','')if ','in str(x)else x) data['Foreign Tuition ($)'] = data['Foreign Tuition ($)'].apply(lambda x:x.replace(',','')if ','in str(x)else x) data['Local Tuition ($)'] = data['Local Tuition ($)'].apply(lambda x:float(x)) data['Foreign Tuition ($)'] = data['Foreign Tuition ($)'].apply(lambda x:float(x)) subset = data[['Local Tuition ($)','Foreign Tuition ($)']] subset.plot(title = 'The difference between local tuition and foreign tuition')這個圖還是挺簡單的,可以看出來國外學生的學費基本上比本國學生的學費要高。
5.用pyecharts畫更精細的圖。
#用昨天剛學的pyecharts試試 from pyecharts import Bar#x軸是學校名稱 school = data['Business School'] #y軸表示當地人的學費和外國人的學費 local_tuition = data['Local Tuition ($)'] foreign_tuition = data['Foreign Tuition ($)'] #標題和副標題 bar = Bar('Tuition', 'From 25 Business School') #畫第一個當地人的學費的條形圖,用虛線表示平均值,并標注最大和最小值 bar.add('local_tuition',school,local_tuition,mark_line=['average'],mark_point=['max','min']) #畫第二個外國人的學費的條形圖,用虛線表示平均值,并標注最大和最小值,x軸的標注旋轉-45° bar.add('foreign_tuition',school,foreign_tuition,xaxis_rotate = -45,mark_line=['average'],mark_point=['max','min']) bar可以得到這樣一個圖,原圖是動態的(我不知道為什么沒法顯示完整,再研究研究吧)
從這張圖可以知道國外學生的學費和本國學生的學費的最高值都是33060,在International University of Japan (Niigata),最低都是1000,在Jamnalal Bajaj Institute of Management Studies (Bombay)。本國學生學費的平均值為12374.92,外國學生學費的平均值為16581.8。單位都是美元。如果我們想知道國外學生的學費和本國學生的學費的差距是多少,可以再加入一條折線圖。
6.記錄差值
difference = [0 for i in range(len(school))] for i in range(len(school)):difference[i]=foreign_tuition[i]-local_tuition[i] print(difference)7.畫折線圖
from pyecharts import Line,Overlap line = Line() line.add('difference between foreign_tuition and local_tuition ',school,difference)overlap = Overlap() overlap.add(bar) overlap.add(line,yaxis_index=1,is_add_yaxis=True)overlap可以看到有些學校對本國學生和國外的學生一視同仁,學費一樣。有的學校差距就特別大。?
?
8.要求工作經驗和不要求工作經驗的學校學生平均起薪的差別
#要求工作經驗和不要求工作經驗的學校學生平均起薪的差別 #分組 data["Starting Salary ($)"] = data["Starting Salary ($)"].apply(lambda x:x.replace(',','')if ','in str(x)else x) data["Starting Salary ($)"] = data["Starting Salary ($)"].apply(lambda x:float(x)) data["Starting Salary ($)"].head() data.groupby('Work Experience')["Starting Salary ($)"].mean() Work Experience No 24583.333333 Yes 41305.263158 Name: Starting Salary ($), dtype: float64有工作經驗的平均起薪明顯要比沒有工作的平均起薪高得多。?
?
9.要求英語測試和不要求英語測試的學校學生起薪的差別
畫boxplot(箱型圖)
#要求英語測試和不要求英語測試的學校學生起薪的差別 import seaborn as sns from matplotlib import pyplot as plt g = sns.catplot(x="English Test",y="Starting Salary ($)",data=data, kind="box", height = 5 ,palette = "Set1") g.despine(left=True) g.set_xticklabels() plt.title('Boxplot of Starting Salary ($) of English Test',size = 15)?
?可以看出,沒有英語測試的學校,學生起薪差距較大,有英語測試的學校,學生起薪差距較小。沒有英語測試的學校,學生起薪的平均值低于有英語測試的學校的學生起薪的平均值。但是沒有英語測試的學校,學生起薪的最高值要高于有英語測試的學校。個人認為,可能沒有英語測試的學校,門檻更低,可能會招一些素質不好的學生,但是同時有些英語不好但是專業知識好的學生也會招來,因此會有學生起薪差距大的情況。
?
10.起薪和學費的關系
第一個是線性回歸曲線,我不知道怎么加legend
#起薪與學費有關嗎? plt.figure(figsize = (5,5)) g = sns.regplot(x="Local Tuition ($)", y="Starting Salary ($)",color = 'orange', data=data) g2 = sns.regplot(x="Foreign Tuition ($)", y="Starting Salary ($)",color = 'green', data=data) plt.xlabel('tuition')?
?也可以分別畫。
#起薪與學費有關嗎? plt.figure(figsize = (5,5)) g = sns.lmplot(x="Local Tuition ($)", y="Starting Salary ($)", data=data) g2 = sns.lmplot(x="Foreign Tuition ($)", y="Starting Salary ($)", data=data)?
可以發現起薪和學費基本上是正相關的。
THE END.?
總結
以上是生活随笔為你收集整理的【python数据分析】亚太地区的商学院(商务与经济统计案例3-3)数据分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用async 解放你的大脑
- 下一篇: 使用data()方法缓存数据