导库
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r'c:\windows\fonts\simsun.ttc',size=12)
plt.style.use('ggplot')
数据来自网络
读取数据
df = pd.read_csv(r'C:\Users\Administrator\Desktop\DataAnalyst.csv',encoding='gbk')
该招聘信息主要包含城市、公司Id、公司规模、公司规模、招聘要求学历、薪资水平等
df.head()
因为positionID是唯一的,可以根据这个特征删除重复行
df_duplicates = df.drop_duplicates(subset='positionId',keep='first')
查看salary字段,发现是一个str值,且是一个范围值,后面需要计算平均薪资,需要先拆分该字段的值并转为数值类型
df_duplicates.salary.head()
0 7k-9k 1 10k-15k 2 4k-6k 3 6k-8k 4 2k-3k Name: salary, dtype: object
定义拆分函数
def cut_word(word,method):
position = word.find('-')#找到‘-’符号,存在返回位置索引,不存在返回-1
length = len(word)
if position != -1:#‘3k-4k’字样
bottomSalary = word[:position-1]
topSalary = word[position+1:length-1]
else:#‘3k以上字样’
bottomSalary = word[:word.upper().find('K')]#因为某些行的k是大写,先全转为大写,再定位
topSalary = bottomSalary
if method == 'bottomSalary':
return bottomSalary
else:
return topSalary
取出最小值,新生成一列