分享

小案例:用Pandas分析招聘网Python岗位信息

 大邓的Python 2021-02-23

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

步骤:

  1. 正则表达式抽取出数字列表
  2. 求均值
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. 教育

  1. 设计一个函数,出现正规学历,返回True(包括”不限“)
  2. 使用逻辑索引,把正规学历的招聘信息都保留
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)

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多