[問題] 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