如何将键/值中的列表的嵌套字典解开到列中?我尝试了不同的组合来解决将嵌套字典转换为 pandas 数据框架的问题。通过查看堆栈,我已经接近解决问题,但还没有完全解决。
示例数据:
test = {
'abc': {
'company_id': '123c',
'names': ['Oscar', 'John Smith', 'Smith, John'],
'education': ['MS', 'BS']
},
'DEF': {
'company_id': '124b',
'names': ['Matt B.'],
'education': ['']
}
}
尝试过:
1)
pd.DataFrame(list(test.items())) # not working entirely - creates {dictionary in col '1'}
2)
df = pd.concat({
k: pd.DataFrame.from_dict(v, 'index') for k, v in test.items()
},
axis=0)
df2 = df.T
df2.reset_index() # creates multiple columns
所需输出:
最佳答案
更新:
随着 pandas 0.25
的发布以及 explode
的添加,现在很多变得更加容易:
frame = pd.DataFrame(test).T
frame = frame.explode('names').set_index(
['company_id', 'names'],
append=True).explode(
'education').reset_index(
['company_id', 'names']
)
Pandas 0.25 之前:
这并不是真正的精益,但这是一个相当复杂的转换。
灵感来自this blog post ,我使用两次单独的迭代解决了这个问题,将列表列转换为一系列,然后使用 melt
转换 DataFrame。
import pandas as pd
test = {
'abc': {
'company_id': '123c',
'names': ['Oscar', 'John Smith', 'Smith, John'],
'education': ['MS', 'BS']
},
'DEF': {
'company_id': '124b',
'names': ['Matt B.'],
'education': ['']
}
}
frame = pd.DataFrame(test).T
names = frame.names.apply(pd.Series)
frame = frame.merge(
names, left_index=True, right_index=True).drop('names', axis=1)
frame = frame.reset_index().melt(
id_vars=['index', 'company_id', 'education'],
value_name='names').drop('variable', axis=1).dropna()
education = frame.education.apply(pd.Series)
frame = frame.merge(
education, left_index=True, right_index=True).drop('education', axis=1)
frame = frame.melt(
id_vars=['index', 'company_id', 'names'],
value_name='education').drop(
'variable', axis=1).dropna().sort_values(by=['company_id', 'names'])
frame.columns = ['set_name', 'company_id', 'names', 'education']
print(frame)
结果:
set_name company_id names education
2 abc 123c John Smith MS
6 abc 123c John Smith BS
0 abc 123c Oscar MS
4 abc 123c Oscar BS
3 abc 123c Smith, John MS
7 abc 123c Smith, John BS
1 DEF 124b Matt B.
关于python - Pandas - 从字典中的嵌套键值和嵌套列表创建数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419177/