在数据框中,创建将多列元组作为键并将另一列作为值的字典

问题描述

我有一个以下格式的数据框:

Sr. |  From  | Tran type | Inv type |  Opposite   |    Comment   |
------------------------------------------------------------------
6   | Seller |    X,Y   |   P,Q   |    Buyer    | Rand comment |

“高级”在哪里是df的索引。。df中有几行,每行的“ Tran类型”和“ Inv类型”列中的值数量不同。

我要创建一个字典列表:

{k:v} = {(From,Tran type,Inv type,To) : Comment} 

键本质上是4列的叉积,每个键将只包含一个Tran类型和Inv类型的值。对于上面的示例,它应如下所示:

{
 (Seller,X,P,Buyer): Rand Comment,(Seller,Q,Y,Buyer): Rand Comment
}

这是我现在正在使用的代码

def create_combos_dict(rule):
    tran_types = str(rule['Tran type'])
    tran_types = tran_types.split()
    inv_types = str(rule['Inv type'])
    inv_types = inv_types.split()
    from_side = str(rule['From'])
    opposite = str(rule['Opposite'])
    
    key_tuple = product([from_side],tran_types,inv_types,[opposite])
    combos_dict = {combo:str(rule['Comment']) for combo in key_tuple}
    return combos_dict

mapped_dict = {}
mapped_dict.update(df.apply(create_combos_dict,axis=1))
mapped_dict

但是,这是我得到的输出

{6: {
        (Seller,Buyer): Rand Comment
    }
}

为什么字典由“ Sr”组成。作为键,值是我正在做的实际字典?

目前,我正在遍历combos_dict.values()并将其添加到另一个字典中。但是肯定有一些简单的解决方法吗?

解决方法

我的意思是:

import functools as ft
import pandas as pd

df = pd.DataFrame([[6,"Seller","X,Y","P,Q","Buyer","Rand comment"],[6,"Z,"L,"Rand comment"]],columns = ["Sr.","From","Tran type","Inv type","Opposite","Comment"])

df[["col1","col2"]] = df["Tran type"].str.split(",",expand = True,)
df[["col3","col4"]] = df["Inv type"].str.split(",)
df["col5"] = df.apply(lambda x: {(x["From"],x["col1"],x["col3"],x["Opposite"]):x["Comment"],(x["From"],x["col4"],x["col2"],x["Opposite"]):x["Comment"]},axis = 1)

ft.reduce(lambda total,x: {**total,**x},df["col5"])

输出:

{('Seller','X','P','Buyer'): 'Rand comment',('Seller',' Q',' Y','Z','L','Buyer'): 'Rand comment'}