python - 将带有字典子列表的字典转换为 pandas 数据框
问题描述
我有这个带有字典“dict”的代码:
import pandas as pd
dict = {
'2000': [{'team': 'Manchester United', 'points': '91'}],
'2001': [{'team': 'Manchester United', 'points': '80'}],
'2002': [{'team': 'Arsenal', 'points': '87'}]
}
df = pd.DataFrame.from_dict(dict, orient='index')
print(df)
结果是:
0
2000 {'team': 'Manchester United', 'points': '91'}
2001 {'team': 'Manchester United', 'points': '80'}
2002 {'team': 'Arsenal', 'points': '87'}
但我想要的是:
team points
2000 Manchester United 91
2001 Manchester United 80
2002 Arsenal 87
我想获得这个,而不是在 python 中使用循环,而是使用 pandas。谁能帮我吗?
提前致谢!
解决方案
Tr这个。这将取决于您的数据有多大。
diction = {
'2000': [{'team': 'Manchester United', 'points': '91'}],
'2001': [{'team': 'Manchester United', 'points': '80'}],
'2002': [{'team': 'Arsenal', 'points': '87'}]
}
transformed_dict= {x:d for x,y in diction.items() for d in y }
df = pd.DataFrame(transformed_dict)
df.T
推荐阅读
- python - 在 TensorFlow 中使用 stop_gradient 和 AdamOptimizer
- javascript - EcmaScript6 findIndex 方法,它可以返回多个值吗?
- python-3.x - networkx 中 Graph 的 nbunch 的加权最小生成树
- button - odoo 8 按钮逻辑
- python - 如何在不更改 matplotlib 中的图形大小的情况下在图形下方显示图例
- regex - ls 所有不匹配多个扩展名的目录
- asp.net - 如何在初始池线程之外对非池线程执行 ASP.NET Core 执行?
- sql-server - SQL Server SSL + TDE 与始终加密
- android - Play 商店 Alpha/Beta 选择加入链接是否相同?
- angular - 标签未触发和事件未在 Angular 4 中构建的单页 Web 应用程序中注册