码迷,mamicode.com
首页 > 其他好文 > 详细

pandas 初识(四)

时间:2019-04-29 21:01:26      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:查询   obj   and   request   none   orm   set   float   form   

Pandas 和 sqlalchemy 配合实现分页查询 Mysql 并获取总条数

@api.route(/show, methods=["POST"])
def api_show():  # 分页查询并获取总数
    offset = request.json.get(offset, 0)
    limit = request.json.get(limit, 10)
    sql = "select SQL_CALC_FOUND_ROWS * from bidata.gen_adid_cost order by id desc limit {offset},{limit}".format(
        offset=offset,
        limit=limit)
    con = db.get_engine(current_app, tm_new_hfjy)
    with con.connect() as conn:
        df1 = pd.read_sql(sql, con=conn)
        df2 = pd.read_sql("SELECT FOUND_ROWS() as total;", con=conn)

        # df1 = df1.drop(index=[0])
        # print(df1["update_time"].head())
        # print(df1.dtypes)
        # df1["create_time"] = df1["create_time"].dt.strftime("%Y/%m/%d")

        total = df2.loc[0, "total"]
   df = df.where(df.isnull(), None)
    result = list(df1.T.to_dict().values())

    import numpy as np

    class MyNpEncoder(json.JSONEncoder):
        def default(self, obj):
            if isinstance(obj, np.integer):
                return int(obj)
            elif isinstance(obj, np.floating):
                return float(obj)
            elif isinstance(obj, np.ndarray):
                return obj.tolist()
            elif isinstance(obj, pd.datetime):
                return obj.strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(obj, date):
                return obj.strftime("%Y/%m/%d")

            else:
                return super(MyNpEncoder, self).default(obj)

    data = dict(code=RET.OK, msg="共 %s 条,本页加载 %s 条记录!" % (total, len(result)), data=result, total=total)
    return json.dumps(data, cls=MyNpEncoder)

 

pandas 初识(四)

标签:查询   obj   and   request   none   orm   set   float   form   

原文地址:https://www.cnblogs.com/spaceapp/p/10792660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!