Skip to content
Advertisement

Transpose 3 column excel with K:V into column Pandas

I have a 3 column excel file I’m reading into pandas with basically k:v pairs in columns

JavaScript

I need to not only tie the information in unnamed:1 & unnamed:2 to the unique animal ID as this is how I will track the animal but also transpose these columns where everything to the left of the “:” is the column header w/ appropriate values.

JavaScript

Currently working off of

JavaScript

but it gives this waterfall output (expectedly so)

JavaScript

Advertisement

Answer

Each section starts with animal, so create a counter that acknowledges that. the counter will be used in pivoting the data:

JavaScript

Here we split the column with : and pivot the data; the counter ensures a unique index, which is required for a successful pivot:

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