数据分析--美国科技公司员工满意度分析

(参考:小象学院公开课,网络文献)
1、分析目的:通过对美国各科技公司员工的匿名评价,分析员工满意度;
2、数据来源:Glassdoor网站(类似于国内的看准等);
3、数据大小:37576kb,共67k条数据;
4、使用的分析工具:jupyter notebook
5、使用到的python库:pandas、matplotlib、seaborn、wordcloud
6、分析思路:先通过pandas获取数据后,对数据进行预览;然后对数据进行预处理——如格式转换,去空值等;再通过对数值数据进行处理,绘制图表;最后对于文本数据进行拼接,绘制词云。

一、数据的载入与预览
首先载入相关的库,读取数据:

import pandas as pd
data_file = './dataset/employee_reviews.csv'
data_df = pd.read_csv(data_file)

对数据进行一个整体的概览:

data_df.head()
数据预览

看到数据导入成功了,接下来看一下整体的数据情况:

data_df.info()

数据概览1

可以看到此样本数据共用67529行,16列,其中overall-ratings和helpful-count为数值数据,其他含字符串数据。但是从刚刚的数据预览可以看出从overall-ratings至helpfu-count中间7列应全为数值数据,所以说明中间那5列有干扰数据,需进行处理。
二、数据预处理
首先,先把文本数据和数值数据进行区分:

numeric_data = ['overall-ratings', 'work-balance-stars', 'culture-values-stars', 'carrer-opportunities-stars', 'comp-benefit-stars', 'senior-mangemnet-stars', 'helpful-count']
str_data = ['company', 'location', 'dates', 'job-title', 'summary', 'pros', 'cons', 'advice-to-mgmt', 'link']

然后使用pandas中的to_numeric方法将numeric_data中的数据进行转换,非数值数据补为NaN值:

data_df[numeric_data] = data_df[numeric_data].apply(pd.to_numeric, errors = 'coerce')

再看一下数据的整体情况:


数据概览2

发现之前那5列为object的数据已经变成了float格式,但是其数据的量都小于67529了,最小的只有53983条,可见里面的异常数据很多,为了更加直观再用head预览一下前20行的数据:

数据预览2

接下来使用fiilna方法对于NaN进行填充,统一填充为0:

data_df[numeric_data] = data_df[numeric_data].fillna(0)

三、数据可视化
先引入相关的库:

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline   
#直接在jupyter中展现画布

看一下各个公司总体评论数量的情况,因为数据中有分前员工和现员工的,所以在表格里面新建一列,用于区分员工的状态:

data_df['employee_status'] = ' '
data_df.loc[data_df['job-title'].str.contains('Current'), 'employee_status'] = 'Current'
data_df.loc[data_df['job-title'].str.contains('Former'), 'employee_status'] = 'Former'

然后用sns中的countplot方法绘制计数图:

plt.figure(figsize = (8, 5))
sns.countplot(x = 'company', data = data_df, hue = 'employee_status')
plt.title('Records comparison of Current & Former')
plt.show()
计数图

接下来就开始看评分情况了,先看一下总体的评分,也就是overall-ratings这一列,取平均值:

plt.figure()
data_bar = data_df.groupby('company')['overall-ratings'].mean()
companies = pd.unique(data_df['company'])
plt.title('Overall-ratings')
plt.xlabel('company')
plt.ylabel('ratings')
plt.bar(companies , height = data_bar, width = 0.5, color = ['b', 'g', 'r', 'y', 'm', 'k'])
plt.show()
总体评分

可以看出facebook最高,但是鉴于facebook的样本量靠后,综合靠前的应该是亚马逊。
当然也可以对其他列的评分进行一个统计分析,我们使用一个for循环来进行:

ratings_data = ['overall-ratings', 'work-balance-stars', 'culture-values-stars', 'carrer-opportunities-stars', 'comp-benefit-stars', 'senior-mangemnet-stars']
for ratings_datas in ratings_data:
    plt.figure()
    data_figure = data_df.groupby('company')[ratings_datas].mean()
    companies = pd.unique(data_df['company'])
    plt.bar(companies , height = data_bar, width = 0.5, color = ['b', 'g', 'r', 'y', 'm', 'k'])
    plt.xlabel(ratings_datas)
    plt.show()

结果就不展示了,Facebook一枝独秀。
因为刚刚数据里面就区分在职员工和离职员工,接下来看一下他们的评价有什么不同。
通过构建一个新表,进行绘制:

results = data_df.groupby(['company', 'employee_status'])['overall-ratings'].mean()
current_mean_ratings = results[:, 'Current']
former_mean_ratings = results[:, 'Former']
mean_ratings = pd.DataFrame()
mean_ratings['Current Mean Ratings'] = current_mean_ratings
mean_ratings['Former Mean Ratings'] = former_mean_ratings
companies = pd.unique(data_df['company'])
plt.figure(figsize = (10, 6))
ax = plt.subplot(111)
ax.set_xticklabels(companies, fontsize = 12)
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.xlabel('公司名称',size = 15)
plt.ylabel('评分',size = 15)
plt.title('各科技公司现员工与前员工评分的对比', size = 18)
plt.bar(companies, height = mean_ratings['Current Mean Ratings'], width = 0.4, label = '现员工')
f_xlim = [0.5, 1.5, 2.5 ,3.5, 4.5, 5.5]
plt.bar(f_xlim, height = mean_ratings['Former Mean Ratings'], width = 0.4, label = '前员工')
plt.legend(fontsize = 12)
plt.show()
在职员工和离职员工的评价对比

此图可以看出,离职的员工的评分都比在职的员工低,这也符合正常的逻辑,如果认为这个公司好怎么还会离职呢?

下面对于文本进行分析:
首先导入库,然后使用for循环,将不同的公司进行区分,并使用join函数将同一公司的summary进行合并,最后绘制词云进行展示。

from wordcloud import WordCloud
companies = pd.unique(data_df['company'])
#interpolation参数是用于设置边界模糊度
for company in companies:
    company_df = data_df.loc[data_df['company'] == company]
    company_review = ' '.join(str(review) for review in company_df['summary'])
    wordcloud = WordCloud().generate(company_review)
    plt.imshow(wordcloud, interpolation = 'bilinear')
    plt.axis('off')
    plt.title(company)
    plt.show()

部分效果图

四、学习总结
这一次学习主要是了解怎么样来做数据分析:
1、通过head、info、describe等进行数据的预览,发现异常数据。
2、发现异常数据后进行处理,这是使用了to_numeric方法将含有异常数据的列转变成全为数值类型;然后使用fillna对空值填充为0,具体应该填充什么,应考虑实际的需求,常见的还有dropna还有空值的行进行删除,填充均值,插值等方法。
3、对数据进行可视化,提取信息,这里通过seaborn、pyplot、worldcloud对数据进行可视化,查看各公司员工的满意度。当然还可以使用机器学习相关知识对数据进行分析,提取更多信息,这个还没有学习太多,就不做了。
4、其他技巧,这里学习到了通过构建新的列,来进行数据分析的方法,到现在为止看到的几个案例中,这个技巧还是挺常用的。

你可能感兴趣的:(数据分析--美国科技公司员工满意度分析)