Skip to content
Advertisement

How to turn a Pandas DataFrame into a table of vectors

I have a two columns Pandas data frame containing a list of user_ids and some URLs they have visited. It looks like this:

JavaScript

I want to create a vector representation of itself, like this:

JavaScript

I’ve tried different things, but keep hitting a wall. Any ideas?

Advertisement

Answer

What you’re describing is a pivot of the url column

JavaScript

This puts the users column in the index, but you can use reset_index to make it a regular column again.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement