本文介绍了将数据框转换为具有多个值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数据框
Sr.No ID ABCD
1 Tom Earth English BMW
2汤姆·马斯(Tom Mars)西班牙宝马绿色
3迈克尔·水星印地语奥迪黄色
4约翰·维纳斯葡萄牙梅赛德斯蓝色
5约翰·德国奥迪红色
我正在尝试通过ID将其转换为字典,例如:
{'ID':'Tom','A':['Earth','Mars'],'B':['English','Spanish'],'C':
['BMW' ,'BMW'],'D':['Green']}},
{'ID':'Michael','A':['Mercury'],'B':['印地语'],'C':['Audi'],
'D':['Yellow']},
{'ID':'John','A': ['Venus'],'B':['Portugese','German'],'C':
['Mercedes','Audi'],'D':['Blue','R ed']}
我也尝试过,
df.set_index('ID' ).to_dict()
但这给了我长度为5而不是3的字典。赞赏。
解决方案
按'ID'
分组并应用 to_dict
到每个带有 orient ='list'
的组非常接近:
df.groupby('ID')。apply(lambda dfg:dfg.to_dict(orient ='list'))。to_dict()
Out [25]:
{'John':{'A':['Venus',nan],
'B':['Portugese','German'],
'C':['Mercedes', 'Audi'],
'D':['Blue','Red'],
'ID':['John','John'],
'Sr.No' :[4,5]},
'Michael':{'A':['Mercury'],
'B':['Hindi'],
'C':[ 'Audi'],
'D':['Yellow'],
'ID':['Michael'],
'Sr.No':[3]},
'Tom':{'A':['Earth','Mars'],
'B':['English','Spanish'],
'C':['BMW ','BMW'],
'D':[nan,'Green'],
'ID':['Tom','Tom'],
'Sr.No' :[1,2]}}
应该只是对结果进行略微格式化。 / p>
编辑::从字典中删除'ID'
es:
df.groupby('ID')。apply(lambda dfg:dfg.drop('ID',axis = 1).to_dict(orient ='list'))。to_dict()
Out [5]:
{'John':{'A':['Venus',nan],
'B':['葡萄牙语','德语'],
'C':['Mercedes','Audi'],
'D':['Blue','Red'] ,
'Sr.No':[4,5]},
'Michael':{'A':['Mercury'],
'B':['Hindi'] ,
'C':['Audi'],
'D':['Yellow'],
'Sr.No':[3]},
'Tom ':{'A':['Earth','Mars'],
'B':['English','Spanish'],
'C':['BMW','BMW '],
'D':[nan,'Green'],
'Sr.No':[1、2]}}
I have a dataframe like
Sr.No ID A B C D
1 Tom Earth English BMW
2 Tom Mars Spanish BMW Green
3 Michael Mercury Hindi Audi Yellow
4 John Venus Portugese Mercedes Blue
5 John German Audi Red
I am trying to convert this to a dictionary by ID like :
{'ID' : 'Tom', 'A' : ['Earth', 'Mars'], 'B' : ['English', 'Spanish'], 'C' :
['BMW', 'BMW'], 'D':['Green'] },
{'ID' : 'Michael', 'A' : ['Mercury'], 'B' : ['Hindi'], 'C' : ['Audi'],
'D':['Yellow']},
{'ID' : 'John', 'A' : ['Venus'], 'B' : ['Portugese', 'German'], 'C' :
['Mercedes', 'Audi'], 'D':['Blue', 'Red'] }
This is somewhat similar to what I want.
I also tried ,
df.set_index('ID').to_dict()
but this gives me dictionary of length 5 instead of 3. Any help would be appreciated.
解决方案
Grouping by 'ID'
and apply to_dict
to each group with orient='list'
comes pretty close:
df.groupby('ID').apply(lambda dfg: dfg.to_dict(orient='list')).to_dict()
Out[25]:
{'John': {'A': ['Venus', nan],
'B': ['Portugese', 'German'],
'C': ['Mercedes', 'Audi'],
'D': ['Blue', 'Red'],
'ID': ['John', 'John'],
'Sr.No': [4, 5]},
'Michael': {'A': ['Mercury'],
'B': ['Hindi'],
'C': ['Audi'],
'D': ['Yellow'],
'ID': ['Michael'],
'Sr.No': [3]},
'Tom': {'A': ['Earth', 'Mars'],
'B': ['English', 'Spanish'],
'C': ['BMW', 'BMW'],
'D': [nan, 'Green'],
'ID': ['Tom', 'Tom'],
'Sr.No': [1, 2]}}
It should just be a matter of formatting the result slightly.
Edit: to remove 'ID'
from the dictionaries:
df.groupby('ID').apply(lambda dfg: dfg.drop('ID', axis=1).to_dict(orient='list')).to_dict()
Out[5]:
{'John': {'A': ['Venus', nan],
'B': ['Portugese', 'German'],
'C': ['Mercedes', 'Audi'],
'D': ['Blue', 'Red'],
'Sr.No': [4, 5]},
'Michael': {'A': ['Mercury'],
'B': ['Hindi'],
'C': ['Audi'],
'D': ['Yellow'],
'Sr.No': [3]},
'Tom': {'A': ['Earth', 'Mars'],
'B': ['English', 'Spanish'],
'C': ['BMW', 'BMW'],
'D': [nan, 'Green'],
'Sr.No': [1, 2]}}
这篇关于将数据框转换为具有多个值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!