简体   繁体   English

pandas dataframe to_dict两列作为索引,第三列作为值

[英]pandas dataframe to_dict two columns as indexes and third as value

I have a pandas dataframe as below: 我有一个pandas数据帧如下:

User              ASIN             Rating
A23VKINWRY6J92    1476783284       5
A3HC4SRK7B2AXR    1496177029       5
AE12HJWB5ODOD     B00K2GAUC0       4
AL4RYO265J1G      061579615X       3

I want to generate a dictionary which has 2 columns 'User' and 'ASIN' as keys and third column 'Rating' as the value. 我想生成一个字典,其中有2列'User'和'ASIN'作为键,第三列'Rating'作为值。 Something like below: 如下所示:

my_dict[A23VKINWRY6J92][1476783284] = 5
my_dict[A3HC4SRK7B2AXR][1496177029] = 5
my_dict[AE12HJWB5ODOD][B00K2GAUC0] = 4
my_dict[AL4RYO265J1G][061579615X] = 3

How can I do this? 我怎样才能做到这一点?

Using nested dict comprehension: 使用嵌套的字典理解:

{u: {a: list(df.Rating[(df.User == u) & (df.ASIN == a)].unique()) for a in df.ASIN[df.User == u].unique()} for u in df.User.unique()}

Note that this maps to lists, as there is no reason the resulting value should be unique. 请注意,这会映射到列表,因为没有理由结果值应该是唯一的。

Your question isn't too clear but does this do what you want? 你的问题不太清楚,但这样做你想要的吗?

>>> D = df.groupby(['User','ASIN'])['Rating'].apply(list).to_dict()
>>> {key[0]:{key[1]:val} for key, val in D.items()}
{('A23VKINWRY6J92', '1476783284'): [5], ('A3HC4SRK7B2AXR', '1496177029'): [5], ('AE12HJWB5ODOD', 'B00K2GAUC0'): [4], ('AL4RYO265J1G', '061579615X'): [3]}

So if this is assigned to my_dict then you have 因此,如果将其分配给my_dict那么您就拥有了

>>> my_dict['A23VKINWRY6J92']['1476783284']
[5]

etc 等等

This should work as long as you have unique User ID's. 只要您拥有唯一的用户ID,这应该有效。

my_dict ={d['ASIN'] : {d['User'] : d['Rating']} for d in df.to_dict(orient='records')}

Alternatively you can filter the DataFrame to obtain the rating 或者,您可以过滤DataFrame以获取评级

rating = df.loc[(df['User']=='A23VKINWRY6J92') & (df['ASIN']=='1476783284'), 'Rating'][0]

You can using defaultdict 您可以使用defaultdict

from collections import defaultdict
d = defaultdict(dict)
for _,x in df.iterrows():
    d[x['User']][x['ASIN']] = x['Rating'] 
d=dict(d)
d['A23VKINWRY6J92']['1476783284']
Out[108]: 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM