将数据框转换为具有列表值的字典 [英] Convert Dataframe to a Dictionary with List Values
本文介绍了将数据框转换为具有列表值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个 Dataframe df :
Label1 Label2 Label3
key1 col1value1 col2value1
key2 col1value2 col2value2
key3 col1value3 col2value3
dict1 = df.set_index('Label1').to_dict()
当我们有2列时,此方法有效.
This works when we have 2 columns..
预期输出:
my_dict = {key1: [col1value1,col2value1] , key2: [ col1value2,col2value2] , key3:[col1value3,col2value3] }
我可以在数据框 df 上使用to_dict
以具有 2个其他列作为值的键,以列表的形式?
Can I use to_dict
on Dataframe df to have a key with 2 other columns as values in form of list ??
推荐答案
那么您可以使用字典理解和迭代:
Well you could use a dictionary comprehension and iterrows:
print {key:row.tolist() for key,row in df.set_index('Label1').iterrows()}
{'key3': ['col1value3', 'col2value3'],
'key2': ['col1value2', 'col2value2'],
'key1': ['col1value1', 'col2value1']}
此外,我认为以下内容也可以使用:
Also, I think the following will work too:
df = df.set_index('Label1')
print df.T.to_dict(outtype='list')
{'key3': ['col1value3', 'col2value3'],
'key2': ['col1value2', 'col2value2'],
'key1': ['col1value1', 'col2value1']}
截至2017年秋季的更新; outtype
不再是关键字参数.改用orient:
Update as of fall 2017; outtype
is no longer the keyword argument. Use orient instead:
In [11]: df.T.to_dict(orient='list')
Out[11]:
{'key1': ['col1value1', 'col2value1'],
'key2': ['col1value2', 'col2value2'],
'key3': ['col1value3', 'col2value3']}
这篇关于将数据框转换为具有列表值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文