python从中文数字数据区间提取minmax最小值、最大值返回pandas
先上结果:

定义转换函数代码:
def range2min(text):
if '千' in text:
text=text.replace('千','000')#替换中文为数字
if '万' in text:
if '.' in text:
text=text.replace('.','')
text=text.replace('万','000')
else:
text=text.replace('万','0000')
if '元' in text:
text=text.replace('元','')
if '/月' in text:#把月转换
text=text.replace('/月','')
if '/天' in text:#把天转换
text=int(text.split('-')[0])*21.75#日薪转换为月薪
else:
text=text.split('-')[0]
return str(text)#提取-特定字符之前的数字
def range2max(text):
if '千' in text:
text=text.replace('千','000')
if '元' in text:
text=text.replace('元','')
if '/月' in text:#把月转换
text=text.replace('/月','')
if len(text.split('-'))>1:#判断确实是区间、
if '万' in text:
if '.' in text.split('-')[1]:
text=text.replace('.','')
text=text.replace('万','000')
else:
text=text.replace('万','0000')
if '/天' in text:
text=text.replace('/天','')
text=int(text.split('-')[1])*21.75
else:
text=text.split('-')[1]
return str(text)#提取-特定字符之后的数字
else:
return text.split('-')[0]
然后应用函数到pandas
data['salary_min']=data['salary'].astype(str).apply(range2min)#对列应用自定义函数
data['salary_max']=data['salary'].astype(str).apply(range2max)
data
原本dataframe:

得到dataframe:
本文作者:孤飞
本文链接:https://www.cnblogs.com/ranxi169/p/16980887.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步