首页 > 解决方案 > 使用 pandas 规范化数据

问题描述

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

数据框 tt

column = 显示字典对的基础值

我想创建一个数据框作为输出,从底层取出密钥并针对每个企业 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

标签: pandas

解决方案


合成数据。 explode()然后列表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               []

推荐阅读