gpt4 book ai didi

python - 在 python 中将特定字段从数据帧转换为字典

转载 作者:行者123 更新时间:2023-12-01 02:00:47 25 4
gpt4 key购买 nike

我有以下数据:

Parent_Attribute    Attribute       Y/N for Modelling   Impute_Value    Type    Product     Description
age age YES Unknown CAT Multiple ecohorts
bill_other bill_other_m0 YES 0 CAT Multiple Billing (other)
bill_other bill_other_m1 YES 0 CAT Multiple Billing (other)
bill_other bill_other_m2 YES 0 CAT Multiple Billing (other)
bill_other bill_other_m3 YES 0 CAT Multiple Billing (other)
bill_other bill_other_m4 YES 0 CAT Multiple Billing (other)

所有这些都需要存储在字典中,其中属性是键,其余列值作为该键的值:

attribute : ['parent_attribute,Y/N Modelling,Impute_Value,Type,Product,Description] 

必须对数据帧的每一行执行此操作。

完成的词典将如下所示。

{                       
age :['age',"Yes","Unknown","CAT","Multiple","ecohorts"]
bill_other_m0 :["bill_other","Yes",0,"CAT","Multiple","Billing(Other)"]
bill_other_m1 :["bill_other","Yes",0,"CAT","Multiple","Billing(Other)"]
bill_other_m2 :["bill_other","Yes",0,"CAT","Multiple","Billing(Other)"]
bill_other_m3 :["bill_other","Yes",0,"CAT","Multiple","Billing(Other)"]
bill_other_m4 :["bill_other","Yes",0,"CAT","Multiple","Billing(Other)"]
}

我浏览了dataframe.to_dict,但似乎仅使用它并不能帮助我实现目的。有人可以指导我吗?

最佳答案

看来需要:

d = df.set_index('Attribute').T.to_dict('l')

关于python - 在 python 中将特定字段从数据帧转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com