Pivot table for non-numerical values using Pandas

Hello friends! Recently I got the requirement to create pivot table for text values to be aggregated and concatenated with ‘,’ for same row label in pandas. It can be easily done using crosstab tool in Alteryx. We’ll be doing that using pandas for the quick tutorial.

Data

The data is the feedback survey form and exported as something like below table. It can be seen as for same ID and Que there are multiple answer_text and we want to pivot and concatenate with ‘,’ for same ID-Que combination.

IdQueanswer_text
Id1Q1High
Id2Q2Low
Id3Q3Medium
Id4Q1Average
Id2Q1Low
Id2Q1High
Id1Q1Medium
Id2Q2Low

Code

Importing libraries

import pandas as pd

Reading data

feedbck = pd.read_csv('feedback.csv')
pivot_table = feedbck.pivot_table(index='Id',
columns = 'Que',
values = 'answer_text',
aggfunc = lambda x: ','.join(str(v) for v in x))

Output

pivot_table
IdQ1Q2Q3
Id1High,Medium
Id2Low,HighLow,Low
Id3Medium
Id4Average

Output to csv

pivot_table.to_csv('output.csv')

Check my Github repository

Keep visiting Analytics Tuts for more tutorials.

Thanks for reading! Comment your suggestions and queries


Leave a Reply

Your email address will not be published. Required fields are marked *