使用熊猫标准化数据

时间:2021-01-09 19:39:40

标签: pandas

我有下面的数据框“tt”,其中第二列“underlier”是字典键列表,其中两个键是 underliersecurityid 和 fxspot

dataframe tt

column = underlier values showing dictionary pair

我想创建一个数据框作为输出,从底层取出键并放置在每个企业 ID 上。例如:

EnterpriseID、underliersecurityid、fxspot

我能够规范底层列本身,但是我不断丢失企业 ID。请建议是否有办法处理这个

tt = bn.iloc[:,[4,-7]]
tt

ttu = pd.DataFrame(bn.iloc[:,-7].values.tolist()).dropna()
ttu
ttu2 = pd.DataFrame(ttu.iloc[:,0].values.tolist()).dropna()
ttu2

1 个答案:

答案 0 :(得分:0)

合成数据。 explode() list 然后在 json_normalize() 的输出上使用 to_dict()dict 扩展为列

tt = pd.DataFrame([{"enterpriseid":"abcd","underlyer":[{"underlyersecurityid":"SWAP10Y","fmspot":[]}]}])

pd.json_normalize(tt.explode("underlyer").to_dict(orient="records"))

输出

enterpriseid underlyer.underlyersecurityid underlyer.fmspot
        abcd                       SWAP10Y               []