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
43
votes
3 answers

How do I melt a pandas dataframe?

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 see…
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
4
votes
4 answers

melt() function duplicating dataset

I have a table like…
Khola
  • 81
  • 10
3
votes
2 answers

How can we melt a dataframe and list words under columns?

I have a dataframe that looks like this. import pandas as pd data = {'clean_words':['good','evening','how','are','you','how','can','i','help'], 'start_time':[1900,2100,2500,2750,2900,1500,1650,1770,1800], …
ASH
  • 20,759
  • 19
  • 87
  • 200
3
votes
2 answers

How to reshape a dataframe with identical column names

I want to reshape this table: Back Vowels x x x y y y a: -26.69 -40.06 -49.59 -15.56 -7.5 -11.89 o: ... ... ... ... ... ... to the following format: Back…
Elena
  • 31
  • 2
3
votes
2 answers

Pandas pivot columns based on column name prefix

I have a dataframe: df = AG_Speed AG_wolt AB_Speed AB_wolt C1 C2 C3 1 2 3 4 6 7 8 1 9 2 6 4 1 8 And I want to pivot it based on prefix to get: df = Speed Wolt C1 C2 C3 Category …
Cranjis
  • 1,590
  • 8
  • 31
  • 64
3
votes
1 answer

Melting multiple columns into one column

I have been trying to melt this columns d = {'key': [1,2,3,4,5], 'a': ['None','a', 'None','None','None'], 'b': ['None','None','b','None','None'],'c':['None','None','None','c','c']} df = pd.DataFrame(d) I need to look like…
3
votes
3 answers

panda-melt is not transposing columns into a sequance

I read a csv it has multiples columns. I want to transpose Date A B C 25/5/2019 25 765.3 896 26/5/2019 98 769 27/5/2019 27.6 453.2 98.6 I have…
Katja
  • 131
  • 1
  • 6
3
votes
2 answers

How to collapse/pivot multiple pandas columns

In the dataset below, # DataFrame using arrays. import pandas as pd import numpy as np # create dataset data = {'Gender':['287F', '287F', '287F', '287F','287F', '287F', '189M', '189M','189M', '189M', '189M', '189F','287M',…
nasa313
  • 266
  • 2
  • 12
3
votes
3 answers

pandas reshaping/transform the dataframe

I have dataframe df: col_name col_month col_value abc 2021-01-31 233 abc 2021-02-28 784 abc 2021-03-31 7868 def 2021-02-28 3652 def 2021-03-31 344 def 2021-04-30 87 I need the result…
sparkstars
  • 87
  • 7
3
votes
3 answers

Complex Pandas Melt

I've got a complex melt function that I need to do. I've tried stepping through it and checking other questions, but I am very stuck! Here is my current dataframe: 1 2 4 5 6 10 24 Userid u_A 0 1 3 0 0 0 5 u_B…
Alex K
  • 129
  • 6
3
votes
4 answers

Stack pandas dataframe

I have a dataframe that I need to stack, melt or unpivot. For each school, I need a new row for each capacity as well as a new column for levels. The levels depends on if the capacity is >0. The city column should be included as well: data =…
2
votes
3 answers

Is it even possible to reshape this using Pandas?

I have this dataframe (with all 50 states and more categories, but if we can get this to work, I can apply it to the whole dataset): US US_bp US_bp% AL AL_bp AL_bp% total_pop 324173084 41393176 12.8 4920613 …
neendoniss
  • 23
  • 4
2
votes
1 answer

join 2 dataframes to get output

Using df1 and df2 below, I would like to create df_new that has feat0, feat1, feat2 from df1 and their corresponding values from df2 as value_feat0, value_feat1, value_feat2. I have also created df_new as expected output. df1 = pd.DataFrame([['Age',…
AviS
  • 23
  • 2
2
votes
4 answers

How do I apply melt / wide_to_long onto a data frame with multiple columns?

I have a one-hot encoded data frame that is similar to the one below: review_score Action & Adventure Classic Movies TV Comedies TV Mysteries 4 0 0 1 0 10 1 0 0 0 5 1 0 0 0 7 0 1 0 0 8 0 0 1 0 7 0 0 0 1 it contains 40+ columns…
Aloysius
  • 33
  • 2
2
votes
2 answers

How to melt and unpivot a multi-header dataframe?

I have this data that I want to unpivot and melt into columns. The data is a multi-header table. I have a sample dictionary of the data. Edit here___ I don't know how to convert a dictionary with multiple keys like I had shown previously into a df…
Shane S
  • 1,747
  • 14
  • 31
1
2 3 4 5 6 7