Showing results for 
Search instead for 
Did you mean: 
Community Team Member
Community Team Member
****The code for this solution is leveraged from this source ***

Let's say you want to pivot a table, but you want the actual data in the pivoted cells to be a string.

Take this SQL output as an example. Note, if your data doesn't already have a rank built in and you want to calculate this instead, use a window function!). We want to transform this table so the ranks are displayed as columns, and the email addresses are displayed as values.
Here is our desired output:
We can accomplish this using the following Python 3.6 code
# SQL output is imported as a pandas dataframe variable called "df"
import pandas as pd

#Pivot the table. Source:
pivot_table = df.pivot_table(index=['platform'],
                 aggfunc=lambda x: ' '.join(str(v) for v in x))

#Reset the index and rename columns
pivot_table = pivot_table.reset_index()
pivot_table.columns = ['platform', 'first place', 'second place', 'third place', 'fourth place', 'fifth place']

Version history
Last update:
‎03-02-2023 09:31 AM
Updated by:
Community Toolbox

Recommended quick links to assist you in optimizing your community experience:

Developers Group:

Product Feedback Forum:

Need additional support?:

Submit a Support Request

The Legal Stuff

Have a question about the Sisense Community?