python 去除列中字符,保留数字

python
如何将图中的salary这一列的数据仅保留数值并完成求中值的计算??
如将10k-16k 转换成 13 ?

图片说明

import re

def cal_value(src):
    regex = r'([\d]*)k-([\d]*)k'
    m = re.search(regex, src)
    values = m.groups()
    return (int(values[0])+int(values[1]))//2

test_str = '10k-16k'
print(cal_value(test_str))

方法1

import pandas as pd
import re
from pandas import DataFrame
salary1 = ["10k-16k","15k-25k","20k-40k","18k-25k","16k-30k"]
df = DataFrame(salary1,columns=["salary"])
regex = '(\d*)k'
mean = []
for i in salary1:
    m = re.findall(regex, i)
    a = (int(m[0])+int(m[1]))/2
    mean.append(a)
df["mean"]=mean

方法2

import pandas as pd
from pandas import DataFrame
salary1 = ["10k-16k","15k-25k","20k-40k","18k-25k","16k-30k"]
df = DataFrame(salary1,columns=["salary"])
df[1] = df.salary.str.replace("k","").str.split("-",expand=True)[0].astype(int)
df[2] = df.salary.str.replace("k","").str.split("-",expand=True)[1].astype(int)
df["mid"] = (df[1]+df[2])/2
df.iloc[:,[0,3]]

结果

图片说明


df = df.copy()#解决SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
#取最低工资&最高工资:
def cut_word(word,method):
    position=word.find('-') #find()函数:寻找某字符串是否在该字符串中出现,如果出现,返回第一次出现的索引,没有的话返回-1
    length=len(word)
    if position !=-1:
        bottomsalary=word[:position]
        topsalary=word[position+1:length-1]
    else:
        bottomsalary=word[:word.upper().find('K')]
        topsalary=bottomsalary
    if method=='bottom':
        return bottomsalary
    else:
        return topsalary
df['bottomsalary']=df.salary.apply(cut_word,method='bottom')
df['topsalary']=df.salary.apply(cut_word,method='top')
#数据类型转换:需要通过赋值才能转换成功
df['bottomsalary']=df['bottomsalary'].astype('float')
df['topsalary']=df['topsalary'].astype('float')