Skip to content Skip to sidebar Skip to footer

Python Pandas Dataframe Pivot Only Works With Pivot_table() But Not With Set_index() And Unstack()

I am trying to pivot following type of sample data in Pandas dataframe in Python. I came across couple of other stackoverflow answers that discussed how to do the pivot: pivot_tabl

Solution 1:

AttributeError: 'NoneType' object has no attribute 'unstack'

When you use inplace = True in set_index it modified the dataframe in place. It doesn't return anything(None). So you can't use unstack on None object.

inplace : boolean, default False

Modify the DataFrame in place (do not create a new object)

Use:

df1 = pdDF.set_index(['items_id','responseTime','label']).unstack('label')    
print(df1)

# Output:

id  responseTime    category_1  category_2 category_3 category_8
ABC  2018-06-24           [3]     [10]         [10]       NULL
DEF  2018-06-25           [7]     NULLNULL       [10]
GHI  2018-06-28NULLNULL         [7]        NULL

Post a Comment for "Python Pandas Dataframe Pivot Only Works With Pivot_table() But Not With Set_index() And Unstack()"