#!/usr/bin/env python # -*- coding:utf-8 -*- # <editable> def execute(): ''' 载入模块 ''' 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", "by": 'score', "ascending": 'True', # ascending这个参数的默认值是True,按照升序排序,当传入False时,按照降序进行排列 "na_position": 'first' # na_position是针对DataFrame中的空缺值的,默认值是last表示将空缺值放在排序的最后,也可以传入first放在最前 } inputs = {"table": 'test'} sql = 'select ' + params['columns'] + ' from ' + inputs['table'] data_in = pd.read_sql_query(sql, engine) ''' 排序 ''' data_out = data_in.sort_values(by=params['by'], ascending=eval(params['ascending']), na_position=params['na_position']) ''' 将结果写出 ''' print(data_out) ''' 数据示例 score 2 NaN 4 4.0 3 5.0 1 20.0 5 20.0 0 80.0 ''' # </editable> if __name__ == '__main__': execute()