zoukankan      html  css  js  c++  java
  • 预处理算法_10_数学函数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    # <editable>
    
    def execute():
        # <editable>
        '''
        导入模块
        '''
        import math
        import pandas as pd
        from sqlalchemy import create_engine
        '''
        连接数据库
        '''
        engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
    
        '''
        选择目标数据
        '''
    
        params = {
            "columns": "score",
            "method": "math.sqrt",  # "向上取整:math.ceil;绝对值:math.fabs;向下取整:math.floor;平方根:math.sqrt;返回整数:math.trunc;"
            "label": 'score'
        }
        inputs = {"table": 'test'}
        sql = 'select ' + params['columns'] + ' from ' + inputs['table']
        data_in = pd.read_sql_query(sql, engine)
        print(data_in)
        '''
        使用数学类函数
        '''
        fun = eval(params['method'])
        if data_in[params['label']].dtypes == 'float64' or data_in[params['label']].dtypes == 'int':
            data_in[params['label']] = data_in[params['label']].apply(lambda x: fun(x))
            data_out = data_in
        else:
            raise ValueError('请选择数值型数据!')
    
        '''
        将结果写出
        '''
        print(data_out)
        '''
        数据示例
           score
        0   80.0
        1   20.0
        2    NaN
        3    5.0
        4    4.0
        5   20.0
        float64
        0    8.944272
        1    4.472136
        2         NaN
        3    2.236068
        4    2.000000
        5    4.472136
        Name: score, dtype: float64
              score
        0  8.944272
        1  4.472136
        2       NaN
        3  2.236068
        4  2.000000
        5  4.472136
    
        '''
    
    # </editable>
    
    if __name__ == '__main__':
        execute()
    作者:沐禹辰
    出处:http://www.cnblogs.com/renfanzi/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    BZOJ2330 SCOI2011糖果
    BZOJ 3812主旋律
    模板更新 扩展卢卡斯
    BZOJ4556 HEOI2016字符串
    CodeForces700E Cool Slogans
    Atcoder Contest069F:Flag
    计算几何模板(更新中......)
    BZOJ4003 JLOI2015城池攻占
    BZOJ3772精神污染
    HDU5919 SequenceⅡ
  • 原文地址:https://www.cnblogs.com/renfanzi/p/14476670.html
Copyright © 2011-2022 走看看