预处理算法_2_类型转换

 


#!/usr/bin/env python
# -*- coding:utf-8 -*-

# <editable>

def execute():
# <editable>
'''
载入模块
'''
import sqlalchemy
# import db_utils
import pandas as pd
from sqlalchemy import create_engine
'''
连接数据库
'''
engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')
'''
选择目标数据
'''
params = {
"columns": "SUNACTIVITY",
"modifi_type": [{'origName': 'SUNACTIVITY', 'origType': 'numeric', 'targetType': 'text', 'otherParam': 0}]


}
inputs = {"table": '纯随机性检验'}
data_sql = 'select ' + params['columns'] + ' from ' + inputs['table']
data_in = pd.read_sql_query(data_sql, engine)
print(data_in.dtypes)

'''
列名为键,类型为值的字典
'''
modifi_type = params['modifi_type']
for i in modifi_type:
if i['targetType'] == 'numeric':
data_in[params['columns']] = data_in[params['columns']].astype("int")
elif i['targetType'] == 'text':
data_in[params['columns']] = data_in[params['columns']].astype("str")
elif i['targetType'] == 'float':
data_in[params['columns']] = data_in[params['columns']].astype("float")
else:
data_in[params['columns']] = data_in[params['columns']].astype("str")

# 写出数据
print(data_in.dtypes)
print(data_in)


# </editable>

if __name__ == '__main__':
execute()
 

 

posted @ 2021-03-03 19:25  我当道士那儿些年  阅读(84)  评论(0编辑  收藏  举报