Python: Convert data from wide to long, wide_to_long, melt ver. 2

Last Updated on September 25, 2021 by shibatau

I.What do you learn?

Let me show you how to convert a data frame from wide to long format. You have to use long formats when you create charts with Plotly and Seaborn.

II.Scripts

1.Using melt

syntax

pandas.melt(df, id_vars=” “, value_vars=” “,
var_name=” “, value_name=’value’)

2.Using wide_to_long

You can easily take care of the prefix in the column names with eide_to_long.

Syntax

pandas.wide_to_long(df, stubnames, i, j, sep=”, suffix=’\\d+’)

The first four parameters must be provied.

All scripts are here:

https://colab.research.google.com/drive/1BJjuWsuoJAaSa_CZk5Y9F0rD0hQBv0ou?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.