合并包含字典的数据框的两列,并将其存储到数据框的另一列中

时间:2020-01-06 05:04:46

标签: python pandas dictionary

我有一个数据框df,其中有两列ab,其中包含字典,我想合并这两个字典并将合并的字典存储在新列{{ 1}}。一个示例数据点是:

c

我有df : a b c ------------------------------------------------------------ {x:{y:{z:u}} {w:{f:{h:l}} {x:{y:{z:u}},{w:{f:{h:l}} a,我想要b。 我有一个将两个字典合并的函数,但是我无法将合并字典分配给列c。

我用于合并两个字典的功能:

c

我正在尝试以下操作,但不起作用:

# Function for merging two dictionaries and adding values if keys are same
def merge_and_add(dict1, dict2):
    # We loop over the key and value pairs of the second dictionary...
    for k, v in dict2.items():
        # If the key is also found in the keys of the first dictionary, and...
        if k in dict1.keys():
            # If  the value is a dictionary...
            if isinstance(v, dict):
                # we pass this value to the merge_and_add function, together with the value of first dictionary with
                # the same key and we overwrite this value with the output.
                dict1[k] = merge_and_add(dict1[k], v)
            # If the value is an integer...
            elif isinstance(v, int):
                # we add the value of the key value pair of the second dictionary to the value of the first 
                # dictionary with the same key.
                dict1[k] = dict1[k] + v
        # If the key is not found, the key and value of the second should be appended to the first dictionary
        else:
            dict1[k] = v
    # return the first dictionary
    return dict1

1 个答案:

答案 0 :(得分:1)

首先,您应该知道字典不是作为函数参数中的值传递的。因此,在您当前的代码中,在创建新的组合字典时会对原始字典进行修改。您可以通过使用词典的副本来处理。

x = {'a':{'b':2}}
y = {'c':{'e':4}}

e = pd.DataFrame({'a':[x], 'b': [y]})

def merge_and_add(x, y):

    dict1 = x.copy()
    dict2 = y.copy()
    # We loop over the key and value pairs of the second dictionary...
    for k, v in dict2.items():
        # If the key is also found in the keys of the first dictionary, and...
        if k in dict1.keys():
            # If  the value is a dictionary...
            if isinstance(v, dict):
                # we pass this value to the merge_and_add function, together with the value of first dictionary with
                # the same key and we overwrite this value with the output.
                dict1[k] = merge_and_add(dict1[k], v)
            # If the value is an integer...
            elif isinstance(v, int):
                # we add the value of the key value pair of the second dictionary to the value of the first 
                # dictionary with the same key.
                dict1[k] = dict1[k] + v
        # If the key is not found, the key and value of the second should be appended to the first dictionary
        else:
            dict1[k] = v
    # return the first dictionary
    return dict1


e['c'] = e.apply(lambda x : merge_and_add(x.a, x.b), axis = 1)

最终输出看起来像

                 a                b                               c
0  {'a': {'b': 2}}  {'c': {'e': 4}}  {'a': {'b': 2}, 'c': {'e': 4}}