预处理算法_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()
作者:沐禹辰
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
出处:http://www.cnblogs.com/renfanzi/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。