使用 Pandas DataFrame 中其他两列的键和值创建字典列 [英] Create column of dictionaries with keys and values from other two columns in Pandas DataFrame

查看:78
本文介绍了使用 Pandas DataFrame 中其他两列的键和值创建字典列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个 Pandas DataFrame,其中有两列,每列包含列表,另一列包含这两个列表的元素的元组对.为方便起见,一个玩具示例类似于:

I currently have a Pandas DataFrame where there are two columns that each contain lists and another column that contains the tuple pairs of the elements of those two lists. A toy example for the sake of convenience would be something like:

        col1       col2       col3       col4
0        'a'       [0, 1]     [8, 9]     [(0, 8), (1, 9)]
1        'b'       [2, 3]     [10, 11]   [(2, 10), (3, 11)]
2        'c'       [4, 5]     [12, 13]   [(4, 12), (5, 13)]
3        'd'       [6, 7]     [14, 15]   [(6, 14), (7, 15)]

我想要做的是创建第 5 列 col5,其中包含以 col3 中的值作为键和 col2 中的值的字典> 作为值.例如:

What I want to do is to create a 5th column col5 that contains dictionaries with the values from col3 as keys and the values from col2 as values. For example:

        col1       col2       col3       col4                 col5
0        'a'       [0, 1]     [8, 9]     [(0, 8), (1, 9)]     {'8': 0, '9': 1}
1        'b'       [2, 3]     [10, 11]   [(2, 10), (3, 11)]   {'10': 2, '11': 3}
2        'c'       [4, 5]     [12, 13]   [(4, 12), (5, 13)]   {'12': 4, '13': 5}
3        'd'       [6, 7]     [14, 15]   [(6, 14), (7, 15)]   {'14': 6, '15': 7}

我尝试过诸如

df['col5'] = dict(zip(df['col4'].apply(ast.literal_eval), df['col3'].apply(ast.literal_eval)))

但我收到一个错误.我处理这个问题的最合适的方式是什么?提前致谢.

but I receive an error. What would be the most appropriate way that I go about this? Thanks in advance.

推荐答案

If laready created col4 is possible loop with map:

If laready created col4 is possible loop with map:

df['col5'] = df['col4'].map(lambda x: [{b:a} for a, b in ast.literal_eval(x)])
print (df)
  col1    col2      col3                col4                col5
0  'a'  [0, 1]    [8, 9]    [(0, 8), (1, 9)]    [{8: 0}, {9: 1}]
1  'b'  [2, 3]  [10, 11]  [(2, 10), (3, 11)]  [{10: 2}, {11: 3}]
2  'c'  [4, 5]  [12, 13]  [(4, 12), (5, 13)]  [{12: 4}, {13: 5}]
3  'd'  [6, 7]  [14, 15]  [(6, 14), (7, 15)]  [{14: 6}, {15: 7}]

或者如果输入是 col3col2 使用带有 list comprehension 的 zip:

Or if inputs are col3 and col2 use zip with list comprehension:

df['col5'] = [dict(zip(ast.literal_eval(a), ast.literal_eval(b))) 
              for a, b in zip(df['col3'], df['col2'])]

print (df)
  col1    col2      col3                col4            col5
0  'a'  [0, 1]    [8, 9]    [(0, 8), (1, 9)]    {8: 0, 9: 1}
1  'b'  [2, 3]  [10, 11]  [(2, 10), (3, 11)]  {10: 2, 11: 3}
2  'c'  [4, 5]  [12, 13]  [(4, 12), (5, 13)]  {12: 4, 13: 5}
3  'd'  [6, 7]  [14, 15]  [(6, 14), (7, 15)]  {14: 6, 15: 7}

或使用 apply 的解决方案:

Or solution with apply:

df['col5'] = (df.apply(lambda x:  dict(zip(ast.literal_eval(x['col3']), 
                                           ast.literal_eval(x['col2']))), axis=1))

这篇关于使用 Pandas DataFrame 中其他两列的键和值创建字典列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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