Pandas : Data Processing and Visulisation with Python (Python Exercise 25)

Data Processing and Visulisation with Python

In this exercise, we will be using the San Francisco city employee salary data from Kaggle! Just follow along and complete the tasks outlined in bold below.

Don’t worry if you can not download the data from Internet right now. I have prepared it for you in file Salaries.csv. Enjoy!

Import pandas as pd.

import pandas as pd

Read Salaries.csv as a dataframe called sal.

sal = pd.read_csv('Salaries.csv')

Check the head of the DataFrame.

sal.head()

在这里插入图片描述

Use the .info() method to find out how many entries there are.

sal.info()

在这里插入图片描述

What is the average BasePay ?

sal.BasePay.mean()

What is the highest amount of OvertimePay in the dataset ?

sal.OvertimePay.max()

What is the job title of JOSEPH DRISCOLL ?

Note:

Use all caps, otherwise you may get an answer that doesn’t match up (there is also a lowercase Joseph Driscoll).

sal.JobTitle[sal['EmployeeName']=='JOSEPH DRISCOLL']

在这里插入图片描述

How much does JOSEPH DRISCOLL make (including benefits)?

sal.TotalPayBenefits[sal['EmployeeName']=='JOSEPH DRISCOLL']

在这里插入图片描述

Who is the highest paid person (including benefits)?

sal.iloc[sal['TotalPayBenefits'].idxmax()]

在这里插入图片描述

Who is the lowest paid person (including benefits)? Do you notice something strange about how much he or she is paid?

sal[sal['TotalPayBenefits']==min(sal.TotalPayBenefits)]

在这里插入图片描述

What was the average (mean) BasePay of all employees per year? (2011-2014) ?

sal.groupby(['Year']).mean()['BasePay']

在这里插入图片描述

How many unique job titles are there?

sal['JobTitle'].nunique()

在这里插入图片描述

What are the top 5 most common jobs?

sal['JobTitle'].value_counts()[:5]

在这里插入图片描述

How many Job Titles were represented by only one person in 2013? (e.g. Job Titles with only one occurence in 2013?)

sal2013 = sal[sal['Year']==2013]
sum(sal2013['JobTitle'].value_counts() == 1)

在这里插入图片描述

How many people have the word Chief in their job title?

Hint:

You can use apply.

sal['JobTitle'].apply(lambda x : 'chief' in x.lower()).sum()

在这里插入图片描述

Bonus: Is there a correlation between length of the Job Title string and Salary?

Hint:

First, generate a new column title_len for length of the JobTitle.
Then, find out the correlation between title_len and TotalPayBenefits.

sal['title_len'] =sal['JobTitle'].apply(lambda x : len(x))
temp=sal[['title_len','TotalPayBenefits']]
temp.corr()

sal['title_len'] =sal['JobTitle'].apply(len)
sal[['title_len','TotalPayBenefits']].corr()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值