[问题] dataframe list中名称变字段

楼主: Pettitte1   2022-09-14 10:33:34
目前我有一个得出的data如下
[Snapshot(ts=1663150448134000000, code='2330', exchange='TSE', open=478.5,
high=482.0, low=476.0, close=481.5, tick_type=<TickType.Sell: 'Sell'>,
change_price=-11.5, change_rate=-2.33, change_type=<ChangeType.Down: 'Down'>,
average_price=479.2, volume=1, total_volume=9175, amount=481500,
total_amount=4396651000, yesterday_volume=16507.0, buy_price=481.5,
buy_volume=160.0, sell_price=482.0, sell_volume=213, volume_ratio=0.56),
Snapshot(ts=1663150456795000000, code='2317', exchange='TSE', open=107.5,
high=108.5, low=107.0, close=108.0, tick_type=<TickType.Sell: 'Sell'>,
change_price=-2.0, change_rate=-1.82, change_type=<ChangeType.Down: 'Down'>,
average_price=107.67, volume=1, total_volume=9599, amount=108000,
total_amount=1033550000, yesterday_volume=32733.0, buy_price=108.0,
buy_volume=565.0, sell_price=108.5, sell_volume=3292, volume_ratio=0.29)]
有两个Snapshot开头的
把他dataframe后
df = pd.DataFrame(snapshots)
会得出以下结果
https://i.imgur.com/1ehIRa8.jpg
但我是想要得到以下结果
https://i.imgur.com/tNLf1iL.jpg
就是把 = 之前的变成字段名称, = 之后的变成值
我看别人colab程式码是直接
df.ts = pd.to_datetime(df.ts)
但我在vscode上直接复制用会跑出错误
AttributeError: 'int' object has no attribute 'lower'
请问该怎么做呢
谢谢
作者: lycantrope (阿宽)   2022-09-14 12:09:00
只有复制贴上,想都没想就上来问喔...上一篇也是也不解释一下Snapshot是怎么产生的通灵就是df=pd.DataFrame(map(dict,snapshots))

Links booklink

Contact Us: admin [ a t ] ucptt.com