数据分析学了几个星期了,中间一直在学习模仿别人的代码,所以没什么原创可写。今天看《商务与经济统计》,想拿个案例来练练手。案例数据也不大:
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.