Python里类似数据库的纵表转横表操作

在使用Python处理MySQL5.6版本的数据时,由于不能进行子查询,在纵表转横表时不是很方便,研究来一下在Python中来实现。类似于这样的转化
Python里类似数据库的纵表转横表操作
ser = df[[‘ctp_account_id’,‘exchange’,‘pos_hold_margin’,‘avail_ratio’,‘trade_date’]]
ser = ser.set_index([‘ctp_account_id’,‘trade_date’,‘exchange’])
ser = ser.unstack(‘exchange’)
ser =ser.reset_index()
print(ser)
Python里类似数据库的纵表转横表操作
Python里类似数据库的纵表转横表操作