如何将内嵌有列表的字典转换为Pandas DataFrame?

huangapple go评论58阅读模式
英文:

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: 
           {&#39;Feb&#39;: [&#39;A&#39;, &#39;B&#39;],
            &#39;Mar&#39;: [&#39;A&#39;, &#39;B&#39;]},
        124:
           {&#39;Feb&#39;: [&#39;C&#39;],
            &#39;Mar&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;]},
        125:
           {&#39;Feb&#39;: [&#39;A&#39;, &#39;C&#39;],
            &#39;Mar&#39;: [&#39;A&#39;]}
                                  }`


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 = [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;]
out = (pd.concat({k: pd.DataFrame.from_dict(v, orient=&#39;index&#39;).T
                for k, v in data.items()}, names=[&#39;Acc Num&#39;])
          .reindex(months, axis=1).fillna(0).droplevel(1).reset_index())

Output:

&gt;&gt;&gt; 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

huangapple
  • 本文由 发表于 2023年4月17日 22:01:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定