首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将特定字段从dataframe转换为python中的字典

将特定字段从dataframe转换为python中的字典
EN

Stack Overflow用户
提问于 2018-04-05 06:58:32
回答 1查看 21关注 0票数 1

我有以下数据:

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
attribute : ['parent_attribute,Y/N Modelling,Impute_Value,Type,Product,Description] 

这必须对dataframe的每一行执行。

完整的字典会看上去像。

代码语言:javascript
复制
{                       
    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,但它似乎不像是只使用它将有助于我的目的。有人能指点我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-05 07:02:50

看来有必要:

代码语言:javascript
复制
d = df.set_index('Attribute').T.to_dict('l')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49665984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档