2

I have dataframe of the form

user_id  item_id  rating
1          abc       5
1          abcd      3
2          abc       3
2          fgh       5

I want to convert it into a numpy matrix such as

# abc  abcd  fgh
[[5,    3,    0]  # user_id 1
[3,    0,    5]] # user_id 2

Can anyone help?

1 Answer 1

3

You can use pivot with fillna, cast to int and last convert to array by values:

arr = df.pivot('user_id', 'item_id', 'rating').fillna(0).astype(int).values
print (arr)
[[5 3 0]
 [3 0 5]]

Another solution with set_index, unstack and values:

arr = df.set_index(['user_id','item_id']).unstack(fill_value=0).values
print (arr)
[[5 3 0]
 [3 0 5]]
Sign up to request clarification or add additional context in comments.

Comments

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.