python str.extract提取小数

注意:
提取小数代码为:

.str.extract('(\d+\.?\d*)', expand=False)

提取整数代码为:

.str.extract('(\d+)', expand=False)

读取某列信息,且仅保留数字

# 读取这一列中的信息,保留数字
def numOnly(col_names):
    for col_name in col_names:
        b[col_name] = b[col_name].str.extract('(\d+)', expand=False)
        
    index_start = b.columns.get_loc("血小板计数")
    index_end = b.columns.get_loc("谷丙转氨酶")
    for column_index in range(index_start,index_end+1):
        b.iloc[:,column_index] = b.iloc[:,column_index].str.extract('(\d+\.?\d*)', expand=False)
        
    index_start = b.columns.get_loc("手术_出血量ml")
    index_end = b.columns.get_loc("详情_Apgar评分(高)")
    for column_index in range(index_start,index_end+1):
        b.iloc[:,column_index] = b.iloc[:,column_index].str.extract('(\d+\.?\d*)', expand=False)
    return True

你可能感兴趣的:(实践,python,开发语言)