pandas - 使用 pandas 规范化数据
问题描述
我有下面的数据框'tt',其中第二列'underlier'是字典键列表,其中两个键是underliersecurityid和fxspot
我想创建一个数据框作为输出,从底层取出密钥并针对每个企业 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
解决方案
合成数据。 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 []
推荐阅读
- git - git-remote-https 无法分叉加载到不同地址的 msys-unistring-2.dll
- javascript - 在asp.net mvc中使用jquery从html文本框加载HTML gridview
- javascript - 如何编写从 jsfiddle 到本地主机的日历
- ruby-on-rails - Rails 不断询问我“请在尝试启动应用程序之前运行 `bundle install`”
- python - Python将txt文件转换为列表-优化
- sql-server - 存储过程在执行时运行,但即使成功也不会在代理作业中运行
- c++ - 继承自不完整类的类模板
- r - 在 R 中使用 LDA 按组进行主题建模
- java - 在 android 7 或更高版本中自动安装应用程序
- algorithm - 是否可以通过只知道结果数组的未知数组找到一个未知常数异或?