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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
  • 相关阅读:
    万丈高楼平地起
    @synthesis 使用的时候注意的地方
    arc4random()
    Whereami: CLLocationManager not calling delegate
    总结
    生病两日,真是难受
    Xcode 5 Error CertUIFramework.axbundle
    c++笔记 重要的声明
    class
    检查没有错误或警告
  • 原文地址:https://www.cnblogs.com/renfanzi/p/14476670.html
Copyright © 2011-2022 走看看