Questions tagged [melt]

In R, the "melt" function from the "reshape2" and "data.table" (and earlier, "reshape") packages converts data into a long form. Melt is also a web application framework written in PHP.

In data processing, reshaping data to a long form where each row represents only one observation of one variable is often called "melting" the data, similar to UNPIVOT in some relational databases such as .

In , the function is part of the and packages (earlier, the "reshape" package).

This functionality is also found in similar data processing tools such as .

Related tags:

840 questions
103
votes
3 answers

Opposite of melt in python pandas

I cannot figure out how to do "reverse melt" using Pandas in python. This is my starting data label type value 0 x a 1 1 x b 2 2 x c 3 3 y a 4 4 y b 5 5 y c 6 6 z a …
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
65
votes
6 answers

How to melt Spark DataFrame?

Is there an equivalent of Pandas Melt function in Apache Spark in PySpark or at least in Scala? I was running a sample dataset till now in Python and now I want to use Spark for the entire dataset.
65
votes
2 answers

reshape2 melt warning message

I'm using melt and encounter the following warning message: attributes are not identical across measure variables; they will be dropped After looking around people have mentioned it is because the variables are different classes; however, that is…
cherrytree
  • 1,561
  • 3
  • 16
  • 33
58
votes
3 answers

Melt the Upper Triangular Matrix of a Pandas Dataframe

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 a b .5 a …
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
48
votes
3 answers

Reshape wide to long in pandas

Let's assume that I have the following dataframe in pandas: AA BB CC date 05/03 1 2 3 06/03 4 5 6 07/03 7 8 9 08/03 5 7 1 and I want to transform it to the following: AA 05/03 1 AA 06/03 4 AA 07/03 …
km1234
  • 2,127
  • 4
  • 18
  • 21
46
votes
3 answers

How to melt and cast dataframes using dplyr?

Recently I am doing all my data manipulations using dplyr and it is an excellent tool for that. However I am unable to melt or cast a data frame using dplyr. Is there any way to do that? Right now I am using reshape2 for this purpose. I want 'dplyr'…
Koundy
  • 5,265
  • 3
  • 24
  • 37
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
43
votes
5 answers

Reshaping wide to long with multiple values columns

I need to reshape my wide table into long format but keeping multiple fields for each record, for example: dw <- read.table(header=T, text=' sbj f1.avg f1.sd f2.avg f2.sd blabla A 10 6 50 10 bA B 12 5 70 11 …
Vasily A
  • 8,256
  • 10
  • 42
  • 76
38
votes
3 answers

Pandas DataFrame stack multiple column values into single column

Assuming the following DataFrame: key.0 key.1 key.2 topic 1 abc def ghi 8 2 xab xcd xef 9 How can I combine the values of all the key.* columns into a single column 'key', that's associated with the topic value…
borice
  • 1,009
  • 1
  • 8
  • 15
36
votes
2 answers

How can I "unpivot" specific columns from a pandas DataFrame?

I have a pandas DataFrame, eg: df = pd.DataFrame({'farm' : ['A','B','A','B'], 'fruit':['apple','apple','pear','pear'], '2014':[10,12,6,8], '2015':[11,13,7,9]}) ie: 2014 2015 farm …
Racing Tadpole
  • 4,270
  • 6
  • 37
  • 56
21
votes
4 answers

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

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…
Nick D
  • 430
  • 1
  • 4
  • 11
20
votes
3 answers

Simultaneously melt multiple columns in Python Pandas

wondering if pd.melt supports melting multiple columns. I have the below examples trying to have the value_vars as list of lists but i am getting an error: ValueError: Location based indexing can only have [labels (MUST BE IN THE INDEX), slices of…
Martin Petrov
  • 293
  • 1
  • 3
  • 13
20
votes
1 answer

R package reshape function melt error: id variables not found in data when working with a lot of factors

I am working with a rarefaction output from mothur, which basically gives me a dataset containing the number of sequences sampled and the number of unique sequences in several samples. I would like to use ggplot2 to visualize this data and therefore…
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31
19
votes
3 answers

Pandas Melt with Multiple Value Vars

I have a data set which is in wide format like this Index Country Variable 2000 2001 2002 2003 2004 2005 0 Argentina var1 12 15 18 17 23 29 1 Argentina var2 1 3 2 5 7 5 2 Brazil …
LauraF
  • 345
  • 2
  • 5
  • 11
19
votes
3 answers

R cannot melt data.frame

I have the following data.frame, called tableMS: X Y Z T 1 375 855 455.7259 3777.856 2 395 969 347.8306 2506.7 3 449 811 309.9512 519.8513 4 451 774 278.291 717.8705 5 453 774 278.291 717.8705 6 455 774 278.291…
Claudia
  • 996
  • 1
  • 10
  • 27
1
2 3
55 56