0
点赞
收藏
分享

微信扫一扫

Python实战—巴尔的摩公务员的待遇怎么样?



本节以美国城市巴尔的摩2016年公务员的工资数据集为例,数据来源于网络,通过数据分组统计,并且可视化,分析其工资情况。


数据来源

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_02



import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline #该数据为美国政府公开的公职人员的薪资数据

salary=pd.read_csv(open('D:\python数据分析\数据\Baltimore_City_Employee_Salaries_FY2016.csv'))
salary.head()

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_03

其中数据集字段Name、JobTitle、AgencyID、Agency、HireDate、AnnualSalary、GrossPay分别代表的含义为姓名、职位名称、工号、单位、入职日期、年薪、总薪资。


问题探索

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_02



  • 年薪的分布情况
  • 公务人员入职日期情况
  • 年薪最高的职务
  • 人数最高的职务


数据清洗

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_02



salary.shape

(13818, 7)

本例含有13818条数据,7列。

salary.isnull().sum()

Python实战—巴尔的摩公务员的待遇怎么样?_柱状图_06

查看缺失值,GrossPay列有272个缺失值。

salary = salary.dropna()
salary.isnull().sum()

Python实战—巴尔的摩公务员的待遇怎么样?_柱状图_07

删除缺失值后的结果。

salary['AnnualSalary'] = salary['AnnualSalary'].str.strip('$')
salary['GrossPay'] = salary['GrossPay'].str.strip('$')
salary[['AnnualSalary','GrossPay']].head()

Python实战—巴尔的摩公务员的待遇怎么样?_数据_08

字符串处理,去掉“$”符号,转换为浮点类型。

salary['GrossPay'].dtype

dtype('O')
salary['AnnualSalary'] = salary['AnnualSalary'].astype(float)
salary['GrossPay'] = salary['GrossPay'].astype(float)
salary['GrossPay'].dtype
dtype('float64')

转换数据类型为浮点类型。

salary['month'] = salary['HireDate'].str.split('/').str[0]
salary[['HireDate','month']].head()

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_09

对于入职日期,新增一列,存放入职月份,把日期数据当作字符串来处理。


数据探索

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_02



salary['AnnualSalary'].hist(bins=20)

Python实战—巴尔的摩公务员的待遇怎么样?_柱状图_11

做出年薪工资的直方图可以发现,年薪基本呈正态分布,但向左略有倾斜,说明高工资的职务还是较少的。

month_count = salary['month'].value_counts()
month_count #对入职的月份计数

Python实战—巴尔的摩公务员的待遇怎么样?_数据_12

month_count.plot(kind='barh')

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_13

对于入职的月份计数后做柱状图,发现入职的高峰期为9月、8月、6月。

agg_salary = salary.groupby('JobTitle')['AnnualSalary'].agg(['mean','count'])
agg_salary #聚合运算,计算各职位的年薪平均值和职位个数

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_14

sort_salary = agg_salary.sort_values(by='mean',ascending=False)[:5]
sort_salary

Python实战—巴尔的摩公务员的待遇怎么样?_数据_15

对年薪平均值降序排列,并取前5。

sort_salary['mean'].plot(kind='bar')

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_16

做出柱状图可以发现,STATE'S ATTORNEY(州检查官)的年薪最高。

sort_count = agg_salary.sort_values(by='count',ascending=False)[:5]
sort_count

Python实战—巴尔的摩公务员的待遇怎么样?_数据_17

对职位计数降序排列,并取前5。

sort_salary['count'].plot(kind='bar')

Python实战—巴尔的摩公务员的待遇怎么样?_数据集_18

做出柱状图可以发现,警察的职位人数远多于其他职位。


Python实战—巴尔的摩公务员的待遇怎么样?_数据_19

举报

相关推荐

0 条评论