简体   繁体   English

如何从 python 中字典列表的特定键创建 dataframe?

[英]How to create a dataframe from specific keys of a List of dictionaries in python?

I have a dictionary like this我有一本这样的字典

data_dict = {'Company': [
                    {
                        'Date': [{'L_End': {'@value': '2013-03-31'},
                                 'L_Start': {'@value': '2006-10-01'},
                                 'Type': {'@value': 'L'}},
                                {'O_Start': {'@value': '2006-10-01'},
                                 'Type': {'@value': 'O'}}],
                       'GeoLoc': {'Location': {'AddrLn1': 'HIGHLAND ROAD',
                                               'Country': 'ENGLAND',
                                               'County': 'HAMPSHIRE',
                                               'PostCode': 'PO49HU',
                                               'Town': 'SOUTHSEA',
                                               'UPRN': Decimal('1775')}},
                       'Name': 'Courtney',
                       'OrgId': {'@assigningAuthorityName': 'ABCDE',
                                 '@extension': 'a12cde',
                                 '@root': '2.16.840.1.1'},
                       'Status': {'@value': 'Active'},
                   },
                  {
                       'Date': [{'L_End': {'@value': '2013-03-31'},
                                 'L_Start': {'@value': '2006-10-01'},
                                 'Type': {'@value': 'L'}},
                                {'O_Start': {'@value': '2006-10-01'},
                                 'Type': {'@value': 'O'}}],
                       'GeoLoc': {'Location': {'AddrLn1': 'VILLIERS ROAD',
                                               'Country': 'ENGLAND',
                                               'County': 'HAMPSHIRE',
                                               'PostCode': 'PO52HG',
                                               'Town': 'SOUTHSEA'}},
                       'Name': 'MERLIN',
                       'OrgId': {'@assigningAuthorityName': 'ABCDE',
                                 '@extension': 'b12cde',
                                 '@root': '2.16.840.1.1'},
                       'Status': {'@value': 'Active'}
                   }
                  ]
}

My expected output is Please Click Image我预期的 output 是请点击图片

I have tried doing this using below code and it works perfectly but I want to see if I can read specific data instead of splitting each row into columns multiple times.我尝试使用下面的代码执行此操作,并且效果很好,但我想看看我是否可以读取特定数据,而不是将每一行多次拆分为列。

df = pd.DataFrame.from_dict(data_dict)
df1 = pd.DataFrame(df.Company.values.tolist())

df2 = pd.concat([df1, df1.OrgId.apply(lambda x: pd.Series(x))], axis=1)
df2 = df2.drop(['OrgId', '@root', '@assigningAuthorityName'], axis=1)
df2 = df2.rename(columns={"@extension": "OrgId"})

df3 = pd.concat([df2, df2.Date.apply(lambda x: pd.Series(x))], axis=1)
df3 = df3.drop('Date', axis=1)

df4 = pd.concat([df3, df3[0].apply(lambda x: pd.Series(x))], axis=1)
df4 = df4.drop([0, 'Type'], axis=1)
....Followed the same until I got all the columns as I needed them to be

df11 = pd.concat([df10, df10.Location.apply(lambda x: pd.Series(x))], axis=1)
df11 = df11.drop('Location', axis=1)

final_df = df11.copy()
print(final_df)

What's the best way to do this?最好的方法是什么?

Try:尝试:

lst = [
    {
        "Name": c.get("Name"),
        "OrgId": c.get("OrgId", {}).get("@extension"),
        "L_Start": next((d for d in c.get("Date", []) if "L_Start" in d), {})
        .get("L_Start", {})
        .get("@value"),
        "L_End": next((d for d in c.get("Date", []) if "L_End" in d), {})
        .get("L_End")
        .get("@value"),
        "Status": c.get("Status", {}).get("@value"),
        **c.get("GeoLoc", {}).get("Location", {}),
    }
    for c in data_dict["Company"]
]

df = pd.DataFrame(lst)
print(df)

Prints:印刷:

       Name   OrgId     L_Start       L_End  Status        AddrLn1  Country     County PostCode      Town  UPRN
0  Courtney  a12cde  2006-10-01  2013-03-31  Active  HIGHLAND ROAD  ENGLAND  HAMPSHIRE   PO49HU  SOUTHSEA  1775
1    MERLIN  b12cde  2006-10-01  2013-03-31  Active  VILLIERS ROAD  ENGLAND  HAMPSHIRE   PO52HG  SOUTHSEA   NaN

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从具有相同键的字典列表中创建熊猫 dataframe。? - How to create a panda dataframe from a list of dictionaries with the same keys.? 如何从 python 中的字典列表中的特定键中提取值? - How to extract values from specific keys from list of dictionaries in python? PYTHON BEGUINNER:如何从python字典列表中创建pandas数据框? - PYTHON BEGUINNER : How to create pandas dataframe from a list of python dictionaries? 如何通过仅使用一些键从另一个字典列表创建字典列表:python - How to create a list of dictionaries from another list of dictionaries by taking only some keys : python 从字典列表创建Pandas Dataframe,其中某些键的值缺失 - Create Pandas Dataframe from List of Dictionaries with missing values for some keys 在熊猫中如何从字典列表中创建数据框? - In pandas how to create a dataframe from a list of dictionaries? 如何从字典列表中的特定值在单独的 Dataframe 列中创建列表? - How do I create a list in a separate Dataframe column from specific values from within a list of dictionaries? Python 从字典列表中获取多个特定的键和值 - Python get multiple specific keys and values from list of dictionaries 如何从键列表和值列表创建字典列表 - How to create a list of dictionaries from a list of keys and a list of values 使用具有重复键的字典列表创建具有唯一键的字典列表 - Create a list of dictionaries with unique keys from a list of dictionaries with duplicated keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM