在数字+字符串的混合数据中提取数字

img


编程刚入门,想要剔除这一列数据中的单位,请问有没有什么办法可以实现呢?

我使用的是df['最大值'].replace(regex=True,inplace=True,to_replace=r'\D',value=r'')
但是得到的结果是

img

其中温度和湿度都是整数,小数点被去除了,有办法解决吗?

import re

def getV(x):
    res = [re.findall(r'.+?(?=[^0-9.])', i)[0] for i in x]
    return res
    
l = ['507ppm', '23.7C', '2184Lux', '88.2%', '2111Lux']

res = getV(l)
print(res)
"""--result
['507', '23.7', '2184', '88.2', '2111']
"""
#全要数字的用下面
def getV(x):
    res = [''.join(j for j in i if j.isdigit()) for i in x ]
    return res
    
l = ['507ppm', '23.7C', '2184Lux', '88.2%', '2111Lux']

res = getV(l)
print(res)
"""--result
['507', '237', '2184', '882', '2111']
"""

直接匹配数字和'.'
df['最大值'].replace(regex=True,inplace=True,to_replace=r'[0-9|.]*',value=r'')