1. 讀取數據
import pandas as pd
import numpy as np
df = pd.read_csv('data/Jobs.csv')
df.head(2)
# 總數
len(df)
356
2. 新增city字段
df['job_area'].unique()
array(['深圳·南山區', '深圳·龍崗區', '深圳', '深圳·福田區', '深圳·光明區', '深圳·龍華區', '深圳·寶安區',
'job_area', '北京', '北京·朝陽區', '北京·海淀區', '北京·通州區', '北京·東城區', '北京·豐臺區',
'北京·大興區', '北京·昌平區', '北京·西城區', '上海', '上海·楊浦區', '上海·浦東新區', '上海·徐彙區',
'上海·長寧區', '上海·青浦區', '上海·靜安區', '上海·普陀區', '上海·黃浦區', '上海·閔行區',
'上海·虹口區', '上海·松江區', '廣州·增城區', '廣州·黃埔區', '廣州·越秀區', '廣州·番禺區',
'廣州·天河區', '廣州', '廣州·海珠區', '廣州·荔灣區', '廣州·白雲區'], dtype=object)
def extract_city(job_area):
if '深圳' in job_area:
return '深圳'
elif '廣州' in job_area:
return '廣州'
elif '北京' in job_area:
return '北京'
elif '上海' in job_area:
return '上海'
else:
return None
extract_city('上海-靜安區')
'上海'
df['job_area'].apply(extract_city)
0 深圳
1 深圳
2 深圳
3 深圳
4 深圳
..
351 廣州
352 廣州
353 廣州
354 廣州
355 廣州
Name: job_area, Length: 356, dtype: object
df['city']=df['job_area'].apply(extract_city)
df.head(2)
3. 三個字段公用一個apply函數
salary
experience
population
步驟:
正則表達式抽取出數字列表
求均值
import re
text = '300-1000人'
def avg(text):
nums = re.findall('\d+', text)
nums = [float(x) for x in nums]
if nums:
return np.mean(nums)
else:
return 0
avg('300-1000人')
650.0
4. 薪資
salary
df['new_salary'] = df['salary'].apply(avg)
df.head(2)
5. 工作年限
experience
df['experience'].apply(avg)
0 2.0
1 4.0
2 0.0
3 7.5
4 4.0
...
351 4.0
352 2.0
353 6.0
354 4.0
355 0.0
Name: experience, Length: 356, dtype: float64
df['new_experience'] = df['experience'].apply(avg)
df.head(2)
6. 員工人數
population
df['population'].apply(avg)
0 10000.0
1 10000.0
2 10000.0
3 10000.0
4 10000.0
...
351 299.5
352 59.5
353 59.5
354 299.5
355 10.0
Name: population, Length: 356, dtype: float64
df['new_population'] = df['population'].apply(avg)
df.head(2)
7. 教育
設計一個函數,出現正規學歷,返回True(包括”不限“)
使用邏輯索引,把正規學歷的招聘信息都保留
df['edu'].unique()
array(['本科', '博士', '碩士', '大專', '不限', 'edu', '6個月', '3個月', '7個月', '4天/周'],
dtype=object)
def edu_bool(level):
if level in ['本科', '博士', '碩士', '大專', '不限']:
return True
else:
return False
edu_bool('博士')
True
df['Edu_bool'] = df['edu'].apply(edu_bool)
df.head(2)
# 邏輯索引
new_df = df[df['Edu_bool']==True]
new_df.head(2)
8. 城市/薪酬關係
city/salary
會用到df.groupby
new_df.groupby('city').mean()
9. 學歷/薪酬關係
edu/salary
會用到df.groupby
new_df.groupby('edu').mean()
10. 城市/學歷/薪酬關係
透視表
pd.pivot_table(df, index, columns, values, aggfunc, margins)
pd.pivot_table(new_df,
index='city',
columns='edu',
values='new_salary',
aggfunc=np.mean,
margins=True)
- END -
往期文章
小案例: Pandas的apply方法
用Python繪製近20年地方財政收入變遷史視頻
Python語法快速入門
Python網絡爬蟲與文本數據分析
讀完本文你就瞭解什麼是文本分析
文本分析在經管領域中的應用概述
綜述:文本分析在市場營銷研究中的應用
從記者的Twitter關注看他們稿件的黨派傾向?
Pandas時間序列數據操作
70G上市公司定期報告數據集
文本數據清洗之正則表達式
shreport庫: 批量下載上海證券交易所上市公司年報
Numpy和Pandas性能改善的方法和技巧
漂亮~pandas可以無縫銜接Bokeh
YelpDaset: 酒店管理類數據集10+G
半個小時學會Markdown標記語法
後臺回覆關鍵詞【崗位分析】下載代碼和數據
點擊左下角可直達B站本文的視頻講解
- END -