Questions tagged [pandas-melt]

Use this tag for questions related to the pandas.melt or pandas.DataFrame.melt. Be sure to also include the [pandas] tag.

Melt can be used to unpivot a DataFrame from wide to long format.

Useful Canonical:

The documentation:

102 questions
1
vote
0 answers

Transpose multiple columns into rows, but convert the transposed columns into one column

With this dataframe: +--------+---------+------+----------+ | metric | control | test | duration | +--------+---------+------+----------+ | logins | 4 | 2 | short | +--------+---------+------+----------+ I want to transpose the control…
bbk611
  • 321
  • 2
  • 10
1
vote
2 answers

Transpose Pandas Dataframe Python

I have this following dataframe: Status Percentage Value Name Tahun 0 X 66.666667 4.0 A 2021 1 Y 33.333333 2.0 A 2021 2 Z 0.000000 0.0 A 2021 0 X 25.000000 2.0 A …
ohai
  • 183
  • 10
1
vote
1 answer

Add label-column to DataFrame

I have two DataFrames for example df1: 0 1 2 3 a 1 2 3 4 b 10 20 30 40 c 100 200 300 400 ------------------ df2: 0 0 x 1 y 2 z Now I want to combine both like: df_new: value label 0 1 x 1 …
olzalk
  • 15
  • 3
1
vote
0 answers

Pandas pivot any columns to rows

Does anyone have any idea how to do this in Pandas? I have this data frame: Uf IBGE CNES 2020 2021 ES 320120 2547678 2747 2916 ES 320120 2485656 1817 2071 ES 320120 3125416 2965 3156 And I need it to look like this: Uf …
TiagoAssun
  • 11
  • 2
1
vote
1 answer

How do I reshape my panda data frame using pivot?

So I have the following code as an exercise and I am trying to melt and unmelt the table using pivot. These are what I have so far: sp500data=yf.Tickers('MMM ABT ...ZTS).history(group_by='ticker',period = '2y') melted =…
user4808494
1
vote
1 answer

How to "Unpivot" multiple columns using melt() function

I need some help to solve this problem. I have a panda dataframe that has 101 unique columns[Column A to Column CW)]. I need to unpivot them into row-based output. Original Data frame; HSP CAT AMK COL OPK ROI GIO DOL …
adey27
  • 439
  • 3
  • 19
1
vote
1 answer

Rearrange Pandas Dataframe using Pandas.Melt to take multiple columns at once?

I have a pandas Dataframe set out as follows. There are in fact 192 X,Y,Z triplet columns, this is just the first three. shot V0e V0n V0d S0_Pe S0_Pn S0_Pd S0_Se S0_Sn S0_Sd 0 1001 457950.4 …
WillH
  • 281
  • 2
  • 13
1
vote
1 answer

InvalidIndexError with melt function Pandas

I have the following dataframe and when I apply melt function: numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64'] df_rm_features_melted = df_rm_features.melt( id_vars=['id', 'date'], …
gunel
  • 161
  • 13
1
vote
1 answer

Pivot data in pandas using melt and unstack

Given the following data: data = pd.DataFrame( { "A": ["a", "a", "b", "b"], "B": ["x", "y", "p", "q"], "C": ["one", "two", "one", "two"], } ) which looks as: A B C 0 a x one 1 a y two 2 b p one 3 b …
baxx
  • 3,956
  • 6
  • 37
  • 75
1
vote
1 answer

Melt column groupings in Pandas

I have a dataframe that looks like this: df1 = pd.DataFrame([('company1',15,20,40,33,5, 9, 81, 34, 7), ('company2', 5,7,10,20,31, 14,18,37,42)], columns=['company','legs - dog', 'head - dog', 'tail - dog','legs - cat', 'head - cat',…
Han
  • 63
  • 3
1
vote
2 answers

Melt/unpivot a dataset with multiple groups of values

I'm trying to convert a dataframe in Python, and I am stuck because I don't know exactly how to phrase what I want to do (which makes searching for it difficult). It seems like I need a combination of unstack and pivot. I can explain it with an…
KJ_
  • 336
  • 1
  • 3
  • 11
1
vote
1 answer

how to convert pandas dataframe to columns in python

I have a dataset given below: weekid A B C D E F 1 10 20 30 40 0 50 2 70 100 0 0 80 0 I am trying to convert given first dataset into another format without including…
user3104352
  • 1,100
  • 1
  • 16
  • 34
0
votes
1 answer

How do I melt and/or pivot my pandas dataframe in way that forces x-axis titles to become index? (see description for visual)

Say I have a df that looks like this: Store Name | 8/7 | 8/14 | 8/21 | 8/28 | more dates... --------------------------------------------- Store 1 | 2.3 | 4.5 | 6.4 | 6.5 | Value.. Store 2 | 5.6 | 6.4 | 5.4 | 4.3 | Value.. Store 3 |…
0
votes
1 answer

Using stack, melt, pivot and other operators together to convert a messy dataframe to an organized one

I'm fairly new to Pandas and often find myself stuck in situations that I have no solution to them rather than asking for help. I have read so many tutorials and checked many Stackoverflow topics but none of them asnwered mine since it is very…
mahb
  • 39
  • 6
0
votes
1 answer

Reshape pandas data frame from long to wide having string/text responses

I have a survey dataset, a part of which I need to expand as columns having textual responses to rating questions asked. The dataset is large, what is the best way of doing this? import pandas as pd pd.DataFrame({'S.No': {0: 63.0, 1: nan, 2:…
Ayan
  • 145
  • 2
  • 16