melt

How to use pd.melt to unpivot a dataframe where columns share a prefix?

How to use pd.melt to unpivot a dataframe where columns share a prefix? Question: I’m trying to unpivot my data using pd.melt but no success so far. Each row is a business, and the data contains information about the business and multiple reviews. I want my data to have every review as a row. My …

Total answers: 3

How to melt a dataframe so repeated items become the values that correspond to the index

How to melt a dataframe so repeated items become the values that correspond to the index Question: I have this dataframe: df = pd.DataFrame({‘Status’:[‘CO’,’AD’,’AD’,’AD’,’OT’,’CO’,’OT’,’AD’], ‘Mutation’:[‘H157Y’,’R47H’,’R47H’,’R67H’,’R62H’,’D87N’,’D39E’,’D39E’]}) print(df) Status Mutation 0 CO H157Y 1 AD R47H 2 AD R47H 3 AD R67H 4 OT R62H 5 CO D87N 6 OT D39E 7 AD D39E I want the …

Total answers: 2

How to perform split/merge/melt with Python and polars?

How to perform split/merge/melt with Python and polars? Question: I have a data transformation problem where the original data consists of "blocks" of three rows of data, where the first row denotes a ‘parent’ and the two others are related children. A minimum working example looks like this: import polars as pl df_original = pl.DataFrame( …

Total answers: 1

Chain df.str.split() in pandas dataframe

Chain df.str.split() in pandas dataframe Question: Edit: 2022NOV21 How do we chain df.col.str.split() since this returns the split columns if expand = True I am trying to split a column after performing .melt(). If I use assign I end up using the original column and the melted column actually does not even exist. df = …

Total answers: 2

How do I melt a pandas dataframe?

How do I melt a pandas dataframe? Question: On the pandas tag, I often see users asking questions about melting dataframes in pandas. I am going to attempt a canonical Q&A (self-answer) with this topic. I am is going to clarify: What is melt? How do I use melt? When do I use melt? I …

Total answers: 3

PySpark Dataframe melt columns into rows

PySpark Dataframe melt columns into rows Question: As the subject describes, I have a PySpark Dataframe that I need to melt three columns into rows. Each column essentially represents a single fact in a category. The ultimate goal is to aggregate the data into a single total per category. There are tens of millions of …

Total answers: 5

Pandas Melt several groups of columns into multiple target columns by name

Pandas Melt several groups of columns into multiple target columns by name Question: I would like to melt several groups of columns of a dataframe into multiple target columns. Similar to questions Python Pandas Melt Groups of Initial Columns Into Multiple Target Columns and pandas dataframe reshaping/stacking of multiple value variables into seperate columns. However …

Total answers: 4

Melt the Upper Triangular Matrix of a Pandas Dataframe

Melt the Upper Triangular Matrix of a Pandas Dataframe Question: Given a square pandas DataFrame of the following form: a b c a 1 .5 .3 b .5 1 .4 c .3 .4 1 How can the upper triangle be melted to get a matrix of the following form Row Column Value a a 1 …

Total answers: 3