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
1
vote
1 answer

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

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…
1
vote
2 answers

How can you group a data frame and reshape from long to wide?

I am fairly new to Python, so excuse me if this question has been answered before or can be easily solved. I have a long data frame with numerical variables and categorical variables. It looks something like this: Category Detail Gender Weight …
csarvf_01
  • 49
  • 4
1
vote
2 answers

How do I convert time series data from wide to long format using python (pandas package)?

I have some data taken at different time points in wide format, and need to convert it to long format to aid with analysis and to merge it with another dataset. The format of the data is (where A_0 means value of A at time 0, A_15 means value at…
1
vote
1 answer

Unpivot the data frame from wide to long in PySpark using melt

I am trying to perform melting operation on my data frame. I have tried the code below, but I am getting an error: A DataFrame object does not have an attribute melt. Please check the spelling and/or the datatype of the object. df_pivot_jp =…
ar_mm18
  • 415
  • 2
  • 8
1
vote
1 answer

Coverting one of the multi-index column labels to a column with row for each value of the column label

My df has multi-index column label like below (six columns): Col lvl 1 01/02/89 31/12/97 15/07/65 Col lvl 2 ID1 ID2 ID1 ID2 ID1 ID2 Apple dec box coy sil mcd les Banana fox dog bil neo…
SakshamC
  • 11
  • 2
1
vote
0 answers

Is there a way to track change in response over time and map that onto changes in response of another category in r?

(I'm SUPER new to coding in general so all suggestions are much appreciated.) So I'm working with a data set that contains panel survey data that was posed to the same 8000 participants 7 times over the course of the last decade. I currently have…
1
vote
0 answers

r - How to reshape data from long to wide format (when you have messy data, many columns, and want to retain some of the original structure)

I have a dataset that I want to convert from long to wide format. I have checked the two most popular answers (here and here), but I can't make sense of them as they apply to my data structure. I want to retain the structure of the dataset but…
aspark2020
  • 341
  • 2
  • 17
1
vote
1 answer

How to substract multiple columns of values in dataframe based on the row being duplicated just once?

Imagine I have a dataframe as such: #Stack example df <- data.frame(DATE = c("2022-08-29", "2022-08-29", "2022-08-29", "2022-08-29", "2022-08-29", "2022-08-29", "2022-09-05", "2022-09-05", "2022-09-05", "2022-09-05",…
Rivered
  • 741
  • 7
  • 27
1
vote
0 answers

Inter-rater reliability of multiple raters responding to (some subset of) multiple questions (in R)

I have data from 5 raters who provided ratings of transcripts by answering up to a dozen questions about each transcript. Each question used a different rating system (e.g., yes vs. no, 1-7, or this vs. that vs. indeterminant). A toy example of the…
Nick Byrd
  • 163
  • 1
  • 14
1
vote
1 answer

Pandas melt automatically sorts the results

I have a dataframe that looks like this: col1 col2 col3 targ1 targ3 targ6 a b c 1 2 3 a c d 2 4 9 I am using melt to pivot the last three columns, and the result looks…
Obiii
  • 698
  • 1
  • 6
  • 26
1
vote
2 answers

preserve index names when melting

I'd like to preserve the proper yearly index names as I recast my data from wide to long. dt = data.table(country = c(1,2,3,4,5), gdp_1990 = rnorm(5), gdp_1991 = rnorm(5), gdp_1992 = rnorm(5), unemp_1990 = rnorm(5), unemp_1991 = rnorm(5),…
Zhaochen He
  • 610
  • 4
  • 12
1
vote
4 answers

Joining or merging multiple columns within one dataframe and keeping all data

I have this dataframe: df = pd.DataFrame({'Position1':[1,2,3], 'Count1':[55,35,45],\ 'Position2':[4,2,7], 'Count2':[15,35,75],\ 'Position3':[3,5,6], 'Count3':[45,95,105]}) print(df) Position1 Count1 …
Cam
  • 111
  • 1
  • 8
1
vote
1 answer

Casting the difference between two vectors into a specified format

I am trying to create code that automatically creates data in the format below (for more background on the colspan see this link). I need to do two things: Calculate the "difference" or "distance" for lack of a better word between two…
Tom
  • 2,173
  • 1
  • 17
  • 44
1
vote
1 answer

How to most efficiently melt a data.frame with multiple measured column groups

I have a data frame of the form: df <- data.frame("id_1" = seq(1,100, 1), "id_2" = seq(1,100, 1), "id_3" = seq(1,100, 1), "m1_g1" = seq(1,100, 1), "m2_g1" = seq(2,200, 2), …
wingsoficarus116
  • 429
  • 5
  • 17
1
vote
2 answers

Convert pandas df from long to wide based on one column as variable and two columns for values

I have a dataframe {'Author': {0: 1, 1: 1, 2: 2, 3: 2}, 'Article': {0: 11, 1: 11, 2: 22, 3: 22}, 'Year': {0: 2017, 1: 2018, 2: 2017, 3: 2018}, 'First': {0: 1, 1: 0, 2: 0, 3: 0}, 'Second': {0: 0, 1: 1, 2: 1, 3: 1}} Want to convert from long to wide…
Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63