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
0
votes
1 answer

unstack/pivot/melt, which one to use?

df=pd.read_csv('https://raw.githubusercontent.com/amanaroratc/hello-world/master/ask_pivot.csv') I have a dataframe of the format: Every date has maximum of two unique Offer_Description. I want to create new columns "Offer_1" and "Offer_2" and…
AmanArora
  • 2,379
  • 6
  • 19
  • 22
0
votes
0 answers

R: Partial melting of columns with syntax pattern

I have a wide R dataframe that I want to partially melt, where the columns all share the same sets of time, and the syntax of the column-names is of a the form (varname_tt_(integer)). Is there a nice way to do the transform below using tidyverse and…
bigO6377
  • 1,256
  • 3
  • 14
  • 28
0
votes
3 answers

How to do a nested 2 level reshape in R?

I am currently working on a survey project that asks about respondents' economic activities. So we will ask the jobs/businesses they have, and since some jobs/businesses are seasonal, we ask about their revenue, cost, and profit in each season. I am…
Jerry Zhu
  • 9
  • 2
0
votes
1 answer

Rearranging data for multi line headers in pandas

I am working with the following data from FWI in pandas I want to transform it to following form for each country I have tried using the melt, stack, and pivot function but have not been able to achieve desired result with these multi line…
MAL
  • 29
  • 4
0
votes
1 answer

Replicate the results from " dcast " function in R using tidyr " pivot " function. Reshape dataframe

I have solved a problem using the dcast function from reshape library. However, I don't usually work with that library and generally I prefer working with dplyr. Here's the data example: segment<- c('seg1', 'seg1', 'seg2', 'seg2', 'seg1', 'seg2',…
lu-202
  • 121
  • 7
0
votes
1 answer

R: Turning row into columns with NA entries (error using tidyr: spread)

I've the following dataset Pet…
Luther_Proton
  • 348
  • 1
  • 7
0
votes
3 answers

Long to wide format using variable names

I have a wide dataset that looks like this: dataset <- data.frame(id = c(1, 2, 3, 4, 5), basketball.time1 = c(2, 5, 4, 3, 3), basketball.time2 = c(3, 4, 5, 3, 2), basketball.time3 =…
Drew
  • 563
  • 2
  • 8
0
votes
1 answer

Converting rows to wide columns based on duplicated ids in another column in pandas

My question is similar to this, this, and this questions. But still cannot resolve it. I have a dataframe with duplicated ids ID Publication_type 1 Journal 1 Clinical study 1 Guideline 2 Journal 2 Letter …
Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
0
votes
1 answer

How to make faceted pie charts for a dataframe of percentages in R?

I have reached to create a melted dataframe containing as values the % of the energy sources (factor variable) for several Years, as additional factor or Date: How could I make nice faceted pie charts for the different years with ggplot (or…
choabf
  • 57
  • 5
0
votes
1 answer

How to widen a data frame using only two of it's variables in R?

I have a 5 column data frame and I want to take that long data frame and make it into a wide data frame by only the last two variables. I would like to keep the first 3 variables in a long format. I want to go from something like this Var1 Var2 …
Alokin
  • 461
  • 1
  • 4
  • 22
0
votes
0 answers

Unable to melt dataframe with multiple headers whose values are not static

I am trying to convert values in the headers of the third and fourth columns as rows df = pd.read_excel("file_name.xlsx", sheet_name = 0, header=[14,15,16,17,18]) df = df.melt(col_level=0, id_vars = ["Unnamed: 0_level_0", "Unnamed: 0_level_1"],…
D M
  • 1
  • 1
0
votes
2 answers

PySpark MapType from column values to array of column name

I've got a dataframe of roles and the ids of people who play those roles. In the table below, the roles are a,b,c,d and the people are a3,36,79,38. What I want is a map of people to an array of their roles, as shown to the right of the…
Sigfried
  • 2,943
  • 3
  • 31
  • 43
0
votes
1 answer

cast data frame to long, dropping rows, expanding others

I have a data frame to pivot to longer: import pandas as pd import io _1 = pd.read_csv(io.StringIO( """date; origin; val_one; val_two; aaa; bbb; ccc; ddd; eee; fff 10/11/2009; aaa; 1; 0; 0; 0; 0; 0; 1; …
Ivo
  • 3,890
  • 5
  • 22
  • 53
0
votes
0 answers

Python melt and concat of multiple columns

|Practice| Geo | Country | Details | Jan | Feb | March| 1Q20| April | May | June | 2Q20 |Year| id| |Cont |America|Chile |Cost |20 |30 |40 |90 |50 |60 |70 |180 |2020| 1 | |Des |America|Chile |Cost |21 |31 …
0
votes
0 answers

multiple column melt and concat function in python

Above is the existing table to which I am trying to melt and bring result as under table. The output I am getting is not coming as per need. Please guide