0

I know this question is simple but I couldn't get it.

I have a dataframe which is called df_check like the one below

enter image description here

I created a dictionary using the code block below

userdic = df_check.groupby('userid')['poiid'].agg(set).to_dict()

userdict output looks like below

print(userdic)

{1337: {9943, 9996, 10202, 10616}, 1339: {10202, 10725,
11601}, 5: {9177, 9489, 10190, 10349}, 54: {123, 4, ...

My goal is to get the values of each key but I have to take them as arrays because I will be working in for loop.

My Solution to get the first key's values.

dict2list = list(userdic.values())[0]
list2array= np.array(dict2list ) 

When I print list2array, I get the result as below.

enter image description here

My expectation like this

[360448, 486401, 190984, 941599, ... ]

but output like this

array({360448, 486401, 190984, 941599, ...},dtype=object)

2
  • 1
    you can just do list(list(userdic.values())[0]), right? Commented Jan 24, 2021 at 15:31
  • set is useful for removing duplicates in the dataset. Commented Jan 26, 2021 at 18:07

1 Answer 1

1

Since you aggregated into a set, your numpy array isn't actually an array. To get the list back you can do list(list2array.item()) and index on that

If you change your list(userdic.values())[0] to list(userdic.values()[0]), your initial list will actually be a list instead of a set and the array will get initiated properly

Sign up to request clarification or add additional context in comments.

5 Comments

When I do print(list2array[0]) I want to see output is 360448 (please see question), I am getting "IndexError: too many indices for array"
The other try, when I do print(dict2list[0]) I want to see the output is again 360448 (please see question), I am getting TypeError: 'set' object does not support indexing
I see. Since you aggregated into a set, your numpy array isn't actually an array. To get the list back you can do list(list2array.item()) and index on that
If you change your list(userdic.values())[0] to list(userdic.values()[0]), both your list and your array should work better
changed my answer to reflect the new information

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.