在python 3中将字典列表转换为单个字典 [英] Converting list of dictionaries into single dictionary in python 3

查看:194
本文介绍了在python 3中将字典列表转换为单个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一小段数据,我需要从中提取特定信息。数据如下所示:

I have a snippet of data from which I need to extract specific information. The Data looks like this:

     pid      log     Date
     91      json     D1
     189     json     D2
     276     json     D3
     293     json     D4
     302     json     D5
     302     json     D6
     343     json     D7

日志是存储在excel文件的一列中的json文件,看起来像这样:

The LOG is a json file stored in a column of an excel file which looks something like this:

{"Before":{"freq_term":"Daily","ideal_pmt":"246.03","datetime":"2015-01-08 06:26:11},"After":{"freq_term":"Bi-Monthly","ideal_pmt":"2583.33"}}

{"Before":{"freq_term":"Daily","ideal_pmt":"637.5","datetime":"2015-01-08 06:26:11"},"After":{"freq_term":"Weekly","ideal_pmt":"3346.88","datetime":"2015-02-02 06:16:07"}}

{"Before":{"buy_rate":"1.180","irr":"31.63","uwfee":"","freq_term":"Weekly"}, "After":{"freq_term":"Bi-Monthly","ideal_pmt":"2583.33"}}

现在,我想要的是这样的输出:

Now, what I want is an output something like this:

    {
     "pid": 91,
     "Date": "2016-05-15 03:54:24"
    "Before": {
        "freq_term": "Daily"
        },
    "After": {
        "freq_term": "Weekly",

        }
}

基本上我只想要 freq_term Datetime 中的之前 之后 。到目前为止,我已经完成了以下代码。之后,无论我做什么,都会给我错误:列表对象不可调用。任何帮助表示赞赏。谢谢。

Basically I want only the "freq_term" and "Datetime" of "Before" and "After" from the log file. So far I have done the following code. After this whatever I do it gives me the error: list object is not callable. Any help appreciated. Thanks.

import pandas as pd

data = pd.read_excel("C:\\Users\\Desktop\\dealChange.xlsx")
df = pd.DataFrame(data, columns = ['pid', 'log', 'date']) 

li = df.to_dict('records')

dict(kv for d in li for kv in d.iteritems()) # error: list obj is not callable 

如何将列表转换成字典,这样我就只能访问所需的数据。

How do I convert the list into a dictionary so that I can access only the data required..

推荐答案

我相信您需要:

df = pd.DataFrame({'log':['{"Before":{"freq_term":"Daily","ideal_pmt":"637.5","datetime":"2015-01-08 06:26:11"},"After":{"freq_term":"Weekly","ideal_pmt":"3346.88","datetime":"2015-02-02 06:16:07"}}','{"Before":{"buy_rate":"1.180","irr":"31.63","uwfee":"","freq_term":"Weekly"}, "After":{"freq_term":"Bi-Monthly","ideal_pmt":"2583.33"}}']})
print (df)
                                                 log
0  {"Before":{"freq_term":"Daily","ideal_pmt":"63...
1  {"Before":{"buy_rate":"1.180","irr":"31.63","u...






首先将值转换为嵌套的字典,然后然后按嵌套的dict理解过滤:


First convert values to nested dictionaries and then filter by nested dict comprehension:

df['log'] = df['log'].apply(pd.io.json.loads)

L1 = ['Before','After']
L2 = ['freq_term','datetime']
f = lambda x: {k:{k1:v1 for k1,v1 in v.items() if k1 in L2} for k,v in x.items() if k in L1}
df['new'] = df['log'].apply(f)
print (df)

                                                 log  \
0  {'After': {'ideal_pmt': '3346.88', 'freq_term'...   
1  {'After': {'ideal_pmt': '2583.33', 'freq_term'...   

                                                 new  
0  {'After': {'freq_term': 'Weekly', 'datetime': ...  
1  {'After': {'freq_term': 'Bi-Monthly'}, 'Before...  

编辑:

用于查找所有具有不可解析值的行可用用法:

For find all rows with unparseable values is possible use:

def f(x):
    try:
        return ast.literal_eval(x)
    except:
        return 1

print (df[df['log'].apply(f) == 1])

这篇关于在python 3中将字典列表转换为单个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆