预处理算法_1_表连接

表连接,主要表达两个DataFrame数据进行拼接

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

# <editable>

def execute():
    # <editable>
    '''
    载入模块
    '''
    import pandas as pd
    from sqlalchemy import create_engine
    '''
    连接数据库
    '''
    engine = create_engine('mysql+pymysql://root:123123qwe@127.0.0.1:3306/analysis')

    '''
    选择目标数据
    '''
    params = {
        "left_columns": "id, score",
        "right_columns": "id, name",
        "left_on": "id",
        "right_on": "id",
        "how": "inner",  # 默认是inner,参数有inner、outer、left、right
    }
    inputs = {"table_left": 'test', "table_right": "class"}

    left_sql = 'select ' + params['left_columns'] + ' from ' + inputs['table_left']
    right_sql = 'select ' + params['right_columns'] + ' from ' + inputs['table_right']
    left = pd.read_sql_query(left_sql, engine)
    right = pd.read_sql_query(right_sql, engine)

    '''
    表连接
    '''
    data_out = pd.merge(left, right,
                        left_on=params['left_on'].split(","),
                        right_on=params['right_on'].split(","),
                        how=params['how'])
    '''
    将结果写出
    '''
    print(data_out)


# </editable>

if __name__ == '__main__':
    execute()

 

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