Home > Blockchain >  create a dictionary value key from dataframe
create a dictionary value key from dataframe

Time:03-10

I have a dataframe :

PageId      OSBrowser
1005581     (11, 16)
1016529     (11, 16)
1016529     (11, 17)
1016529     (12, 14)
1016529     (12, 16)

I am trying to create a dictionary dico : where the key is the OSBrowser value and PageID are the list for each key value :

So the expected result should be :

#key          #values 
(11, 16)       1005581, 1016529
(11, 17)       1016529
(12, 14)       1016529
(12, 16)       1016529

I try with this code :

dico = {}
for row in data:
    tup = data['OSBrowser']
    print(tup)
    if tup not in dico:
        dico[tup] = []
        dico[tup].append(row['PageId'])
        [print(f"{key} : {value}") for key, value in dico.items()]

But I got this error :

----> 5     if tup not in dico:
      6         dico[tup] = []
      7         dico[tup].append(row['PageId'])

TypeError: unhashable type: 'Series'

Can you help me to fix it please?

Thank you

CodePudding user response:

You should use df.index to traverse through the dataframe

d = {}
for ind in data.index:
    page_id = data["PageId"][ind]
    os_browser = data["OSBrowser"][ind]
    if os_browser not in d:
        d[os_browser] = [page_id]
    else:
        d[os_browser].append(page_id)

CodePudding user response:

suppose you define a series like:

s = pd.Series([(11, 16),(11, 16),(11, 17),(12, 14),(12, 14)], index=[1005581,1016529,1016529,1016529,1016529])

you can obtain you expected output with:

s.to_frame().reset_index().groupby(0)["index"].unique()
  • Related