pandas 列索引转换,透视,保留小数点两位,改变列的顺序,改变类型,存文件,数据库读写

import time
from datetime import datetime
from sqlalchemy import create_engine, Column ,Integer,DateTime,DECIMAL
import pandas as pd


answerengine=create_engine('mysql+pymysql://***')
questionengine=create_engine('mysql+pymysql://***')

df = pd.DataFrame({"question_id": [], "is_correct": []})

with answerengine.connect() as conn:
    for i in range(100):
        sql = "select question_id,is_correct from `t_exer_record_{}`".format(str(i))
        df1 = pd.read_sql(sql, conn)
        
        df = pd.concat([df1, df], ignore_index=True, sort=True)
    df = df.pivot_table(index='question_id', aggfunc= "mean").rename(columns={"is_correct":"rate"})

#索引到列转换,列到索引转换
df["question_id"]=df.index
#注意区别df.index=range(len(df))
#这样默认index的名字是“index”而不是ID
df['id']= list(range(len(df)))
df['current_time']=[datetime.now()]*len(df)
df=df.set_index("id")
“”“
注意比较:
qdf=df.groupby("question_id").count().rename(columns={"is_correct":"all_count"})
# ratedf=df.groupby("question_id").sum().rename(columns={"is_correct":"true_count"})
# result = pd.concat([qdf, ratedf], axis=1, join='inner')
# result['rate']=result['true_count']/result['all_count']
”“”

#转换列的类型
df=df.astype({"question_id":"int64"})

#改变列的顺序
df=df[["question_id","rate","current_time"]]

# 结果保留两位小数,以当前时刻存文件
format=lambda x:"%.2f"%x
df["rate"]=df["rate"].map(format)
df.index.astype("int")
res=time.strftime('%Y.%m.%d-%H:%M:%S',time.localtime(time.time()))
name=res+".csv"
df.to_csv(name)

#结果存到数据库
with questionengine.connect() as conn:
    df.to_sql("t_question_rate",con=conn,if_exists='replace',index=True)
    print(df.head())
    print("done")





最后结果

 

 

你可能感兴趣的:(pandas)