Skip to content
Advertisement

pandas groupby create new columns based on col1 containing value of col2

I have a pandas dataframe that I want to group by and create columns for each value of col1 and they should contain the value of col2. And example dataframe:

JavaScript

enter image description here

I want to groupby item_id, create as many columns as feature_category_id and fill them with the feature_value_id.

The resultant df for the example would look like this:

JavaScript

enter image description here

Where features not present for a certain item_id (but present for at least one item_id) are NaN.

Which would be the most optimal operation to do this?

Advertisement

Answer

What you are searching for is pandas pivot() function. It does exactly what you want:

JavaScript

Output:

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