Can not get DataFrame to pivot

Question:

Im am getting data from a tablue worksheet and passing it into a Pandas Data Frame i the need to Pivot the data to be in the needed format how ever does not seem to be changing when run

C Week   Measure Names NT Login        Site Measure Values

0 202306 Calls AHT 1daa04 Derby Sell 0
1 202306 All Calls 1daa04 Derby Sell 972
2 202306 Core Calls 1daa04 Derby Sell 972
3 202306 Glass Calls 1daa04 Derby Sell 0
4 202306 Outbound Calls 1daa04 Derby Sell 9

Need to show as

enter image description here

View data df if the data frame the data is loaded in to, however after the below line is run the Dataframe looks exactly the same

view_data_df.pivot_table(values = 'Measure Values', 
                         index=['C Week','NT Login','Site'],
                         columns = 'Measure Names', 
                         aggfunc=sum).reset_index()
print(view_data_df)
Asked By: Tazbazuk

||

Answers:

Pandas does not usually mutate inplace unless an inplace kwarg is offered.

I think you just need to reassign your dataframe to the pivoted version.

Additionally you should try to use chaining and vertical alignment when possible and practical.

Also to add a code block just use 3 tildas to start and end you code block.

pivot_table_df = (view_data_df
                  .pivot_table(values = 'Measure Values', 
                               index=['C Week','NT Login','Site'],
                               columns = 'Measure Names', 
                               aggfunc=sum)
                  .reset_index()
                  )
print(pivot_table_df)

This very clearly shows that we are starting with view_data_df, making a pivot table, resetting the index, and storing all of those changes in pivot_table_df. Hopefully, this is what you need.

Answered By: georgwalker45
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.