【问题标题】:How to extract dictionary and sub dictionary如何提取字典和子字典
【发布时间】:2020-08-30 08:25:48
【问题描述】:

这是一个由 ID 和 ColumnA 组成的 2 行的原始数据框。某行可能只有一个细节。

ID  ColumnA
1   {'1': {'Order': '0', 'Result': ''},
     '2': {'Order': 'Yellow', 'Result': 'Red'},
     '3': {'Order': 'Clear', 'Result': 'Tight'},
     '4': {'Order': '1.000-1.030', 'Result': '1.015'}}
2   {'1': {'Order': '0', 'Result': '1.015'},
     '4': {'Order': '1.000-1.030', 'Result': '2.4'},
     '5': {'Order': '6.0-7.0', 'Result': ''},
     '6': {'Order': 'Negative', 'Result': 'Negative'},
     '7': {'Order': 'Negative', 'Result': 'Negative'},
     '8': {'Order': 'Negative', 'Result': 'Positive'},
     '9': {'Order': 'Negative', 'Result': ''}}

我想从 ColumnA 提取到新的数据框

ID Column_ID       Column_Order        ColumnD_Result
1  1               0               
1  2               Yellow              Red
1  3               Clear               Tight
1  4               1.000-1.030         1.015
2  1               0                   1.015
2  4               1.000-1.030         2.4
2  5               6.0-7.0                 
2  6               Negative            Negative
2  7               Negative            Negative
2  8               Negative            Positive
2  9               Negative              

提取字典怎么写?

【问题讨论】:

  • 你能显示你的代码吗?如果您包含您的方法并解释为什么不起作用,您更有可能获得高质量的答案

标签: python pandas dataframe dictionary


【解决方案1】:

通过在字典项上循环提取:

import pandas as pd

data = [

    ['1', {'1': {'Order': '0', 'Result': ''},
     '2': {'Order': 'Yellow', 'Result': 'Red'},
     '3': {'Order': 'Clear', 'Result': 'Tight'},
     '4': {'Order': '1.000-1.030', 'Result': '1.015'}}],

    ['2', {'1': {'Order': '0', 'Result': '1.015'},
     '4': {'Order': '1.000-1.030', 'Result': '2.4'},
     '5': {'Order': '6.0-7.0', 'Result': ''},
     '6': {'Order': 'Negative', 'Result': 'Negative'},
     '7': {'Order': 'Negative', 'Result': 'Negative'},
     '8': {'Order': 'Negative', 'Result': 'Positive'},
     '9': {'Order': 'Negative', 'Result': ''}}]]

df = pd.DataFrame(data, columns=['ID', 'ColumnA'])

dfColumnA = pd.DataFrame([], columns=['ID', 'Column_ID', 'Column_Order', 'ColumnD_Result'])

i = 0
for index, row in df.iterrows():
    dictColumA = row['ColumnA']
    for column_ID, v in dictColumA.items():
        dfColumnA.loc[i] = [row['ID'], column_ID, v['Order'], v['Result']]
        i += 1

print(dfColumnA)

输出:

   ID Column_ID Column_Order ColumnD_Result
0   1         1            0               
1   1         2       Yellow            Red
2   1         3        Clear          Tight
3   1         4  1.000-1.030          1.015
4   2         1            0          1.015
5   2         4  1.000-1.030            2.4
6   2         5      6.0-7.0               
7   2         6     Negative       Negative
8   2         7     Negative       Negative
9   2         8     Negative       Positive
10  2         9     Negative               

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    相关资源
    最近更新 更多