1

I want to achive a result such that.

For this dataframe:

Name    Val
   A      1
   A      2
   B      1
   B      3
   B      4

I want a dictionary like {'A':[1,2],'B';[1,3,4]}

I will be having two such dataframes and my goal is to achive the difference in value for each key. Like for key 'B' other data frame can have value as 1,5.

I dont want to use loop as dataframe will be huge.

I tried using

df.set_index(key).to_dict()[value-colum] but this gives me only one value for each key.

I tried to use group by into datafram

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
EXODIA
  • 908
  • 3
  • 10
  • 28
  • This is what I was looking for Adam. Now I just wanna check how can I know the difference in the values of those dictionaries – EXODIA Dec 26 '19 at 13:37

2 Answers2

0

This will covers what you need:

In [22]: out = {}                                                               

In [23]: for i in df["Name"].tolist(): 
    ...:     out[i] = df[df["Name"]==i]["Val"].tolist() 
    ...:                                                                        

In [24]: out                                                                    
Out[24]: {'A': [1, 2], 'B': [1, 3, 4]}
Gokhan Gerdan
  • 1,222
  • 6
  • 19
0

You can try:

ddict = {
  'A':[],
  'B':[]
}

Filter data by column and return values from another column. Set each dictionary key to those results:

ddict['A'] = list(df[df['Name'] == 'A']['Val'].values)
ddict['B'] = list(df[df['Name'] == 'B']['Val'].values)

Output:

{
'A': ['1', '2'],
'B': ['1', '3', '4']
}

Edit: Or, if you want to iterate through all the keys:

 for key in ddict.keys():
    ddict[key] = list(df[df['Name'] == key]['Val'].values)
Mark Moretto
  • 2,344
  • 2
  • 15
  • 21