我有一個看起來像這樣的 DataFrame:
df:
amount info
12 {id:'1231232', type:'trade', amount:12}
14 {id:'4124124', info:{operation_type:'deposit'}}
我想要實作的是:
df:
amount type operation_type
12 trade Nan
14 Nan deposit
我已經嘗試過這種df.explode('info')方法,但沒有運氣。還有其他方法可以做到這一點嗎?
uj5u.com熱心網友回復:
我們可以分兩步完成: (i)df使用data;構建一個 DataFrame (ii)json_normalize在“資訊”列上使用,join然后回傳df:
df = pd.DataFrame(data)
out = df.join(pd.json_normalize(df['info'].tolist())[['type', 'info.operation_type']]).drop(columns='info')
out.columns = out.columns.map(lambda x: x.split('.')[-1])
輸出:
amount type operation_type
0 12 trade NaN
1 14 NaN deposit
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/441360.html
上一篇:從字典中的每個串列中提取第一個值
