How can I reshape my dataset from "Long" format to "Wide" format and otherwise?

If you work with data you probably have come to the scenario where you have found the data you need but not in the shape that you need. A typical example is when data that should be row values are columns or otherwise, a situation that can not be solved by simply transposing the data.

"Wide to Long" and "Long to Wide" steps are useful to solve this.

Untitled Diagram.drawio (2).png

Let's see an example. See the pipeline here

Let's begin by loading some Yahoo Finance data. You will notice the Tickers (Apple, Microsoft, Google. etc) values in the column "Ticker". Let's assume that you need them to be columns, side by side and that you only need the Closing price and not Open-High-Low-Close.

image.png

Next, add a step "Long to Wide (Unstack" to the pipeline.

image.png

And the resulting dataset will look like these

image.png

If you want to reverse this process use the step "Wide to Long" in which case you need to define what will be the name of the column that will now host the tickers and the name of the columns that will have the values. Also you can check and uncheck which columns should be "melted" and uncheck those that will prevail as columns.

image.png

It will now look like these

image.png

Luciano Cohan

Written by

Luciano Cohan

Co-Fundador de Alphacast. Ex Subsecretario de Programación Macroeconómica. Data Science. Creando una plataforma para el trabajo colaborativo en economías

Related insights

  • Read more...

    A short guide to Argentina's Mutual Funds Industry Analysis

    In this short tutorial, we will guide you on calculating ranks of YTD Total Returns for different funds and management companies.

    Alphacast hosts a number of daily updated datasets of Argentinas Mutual Funds Industry. Two weeks ago we began publishing detailed datasets for based on CAFCI daily reports, mostly in

  • Read more...

    A short guide to Argentina's Financial and Monetary Data

    There are more than 2.000 datasets in Alphacast, and there are plenty of hidden gems. This is a short "Must see" guide for those interested in Argentina's financial and monetary data.

    **Would you like to know more?