[英]Convert dictionary with sub-list of dictionaries into pandas dataframe
I have this code with a dictionary "dict":我有这个带有字典“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)
The result is:结果是:
0
2000 {'team': 'Manchester United', 'points': '91'}
2001 {'team': 'Manchester United', 'points': '80'}
2002 {'team': 'Arsenal', 'points': '87'}
But what I want is:但我想要的是:
team points
2000 Manchester United 91
2001 Manchester United 80
2002 Arsenal 87
I would like to obtain this, without using loops in python, and by using pandas.我想获得这个,而不使用 python 中的循环,并使用 pandas。 Can anyone help me out?
谁能帮我吗?
Thanks in advance!提前致谢!
Tr this. Tr这个。 This would depend on how large your data is.
这将取决于您的数据有多大。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.