英文:
How to convert a nested dictionary with a list inside to a Pandas DataFrame?
问题
我正在尝试将这些数据转换成数据框架。。
data = {123:
{'Feb': ['A', 'B'],
'Mar': ['A', 'B']},
124:
{'Feb': ['C'],
'Mar': ['A', 'B', 'C']},
125:
{'Feb': ['A', 'C'],
'Mar': ['A']}
}
期望的输出:
账号编号 | 一月 | 二月 | 三月 |
---|---|---|---|
123 | 0 | A | 0 |
123 | 0 | B | 0 |
123 | 0 | 0 | A |
123 | 0 | 0 | B |
124 | 0 | C | 0 |
124 | 0 | 0 | A |
124 | 0 | 0 | B |
124 | 0 | 0 | C |
125 | 0 | A | 0 |
125 | 0 | C | 0 |
125 | 0 | 0 | A |
我尝试使用`.explode()`,但未获得所需的结果。
<details>
<summary>英文:</summary>
I am trying to convert this data into a dataframe..
`data = {123:
{'Feb': ['A', 'B'],
'Mar': ['A', 'B']},
124:
{'Feb': ['C'],
'Mar': ['A', 'B', 'C']},
125:
{'Feb': ['A', 'C'],
'Mar': ['A']}
}`
Wanted output:
| Acc Num | Jan | Feb | Mar |
| -------- | -------- | --------- | --------- |
| 123 | 0 | A | 0 |
| 123 | 0 | B | 0 |
| 123 | 0 | 0 | A |
| 123 | 0 | 0 | B |
| 124 | 0 | C | 0 |
| 124 | 0 | 0 | A |
| 124 | 0 | 0 | B |
| 124 | 0 | 0 | C |
| 125 | 0 | A | 0 |
| 125 | 0 | C | 0 |
| 125 | 0 | 0 | A |
I have tried to use .explode() and did not get the proper results needed.
</details>
# 答案1
**得分**: 1
以下是您要翻译的代码部分:
```python
months = ['Jan', 'Feb', 'Mar']
out = (pd.concat({k: pd.DataFrame.from_dict(v, orient='index').T
for k, v in data.items()}, names=['Acc Num'])
.reindex(months, axis=1).fillna(0).droplevel(1).reset_index())
输出部分:
>>> out
Acc Num Jan Feb Mar
0 123 0.0 A A
1 123 0.0 B B
2 124 0.0 C A
3 124 0.0 0 B
4 124 0.0 0 C
5 125 0.0 A A
6 125 0.0 C 0
英文:
You can use:
months = ['Jan', 'Feb', 'Mar']
out = (pd.concat({k: pd.DataFrame.from_dict(v, orient='index').T
for k, v in data.items()}, names=['Acc Num'])
.reindex(months, axis=1).fillna(0).droplevel(1).reset_index())
Output:
>>> out
Acc Num Jan Feb Mar
0 123 0.0 A A
1 123 0.0 B B
2 124 0.0 C A
3 124 0.0 0 B
4 124 0.0 0 C
5 125 0.0 A A
6 125 0.0 C 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论