melt

Pandas DataFrame stack multiple column values into single column

时光毁灭记忆、已成空白 提交于 2019-12-28 00:52:31
问题 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 corresponding to the key.* columns? This is the result I want: topic key 1 8 abc 2 8 def 3 8 ghi 4 9 xab 5 9 xcd 6 9 xef Note that the number of key.N columns is variable on some external N. 回答1: You can melt your dataframe: >>> keys = [c for c in df if c.startswith('key.')] >>> pd

How to best reshape a data set in R that has a two-row header?

不打扰是莪最后的温柔 提交于 2019-12-24 16:42:51
问题 The data set I'm working with is in Excel. It shows sales of products in both unit and revenue terms for the first 26 weeks of availability. Each row of data represents a product. Let's say there are 50 of them. The 2nd header row could basically be reconstructed with rep(("Units","Revenue"),26) Above each of those ("Units","Revenue") pairs in the 1st header row is a merged pair of cells taking the sequence "Week 1", "Week 2"...."Week 26". I basically want to convert the dataset from 50 rows

how to use melt and dcast on tough data frame

余生颓废 提交于 2019-12-24 14:18:50
问题 I have a data frame that has one value in each cell, but my last column is a list. Example. Here there are 3 columns. X and Y columns have one value in each row. But column Z is actually a list. It can have multiple values in each cell. X Y Z 1 a d h, i, j 2 b e j, k 3 c f l, m, n, o I need to create this: X Y Z 1 a d h 2 a d i 3 a d j 4 b e j 4 b e k 5 c f l 6 c f m 7 c f n 8 c f o Can someone help me figure this out ? I am not sure how to use melt or dcast or any other function for this.

stacking/melting multiple columns into multiple columns in R

蓝咒 提交于 2019-12-24 11:19:15
问题 I am trying to melt/stack/gather multiple specific columns of a dataframe into 2 columns, retaining all the others. I have tried many, many answers on stackoverflow without success (some below). I basically have a situation similar to this post here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) only many more columns to retain and combine. It is important to mention my year columns are factors and I have many, many more columns than the sample

Melting two sets of two columns into two rows (one row for each column in the set) [duplicate]

孤街浪徒 提交于 2019-12-24 06:11:06
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 5 days ago . I have a data.table as follows: DT <- fread( "ID country year Event_A Event_B Event_A_succ Event_B_succ 4 NLD 2002 0 1 0 0 5 NLD 2002 0 1 0 1 6 NLD 2006 1 1 1 1 7 NLD 2006 1 0 1 0 8 NLD 2006 1 1 0 0 9 GBR 2002 0 1 0 0 10 GBR 2002 0 0 0 0 11 GBR 2002 0 1 0 1 12 GBR 2006 1 1 1 1 13 GBR 2006 1 1 0 1", header = TRUE) I want to cast

In R, plotting wide form data with ggplot2 or base plot. Is there a way to use ggplot2 without melting wide form data frame?

我怕爱的太早我们不能终老 提交于 2019-12-24 02:02:34
问题 I have a data frame that looks like this (though thousands of times larger). df<-data.frame(sample(1:100,10,replace=F),sample(1:100,10,replace=F),runif(10,0,1),runif(10,0,1),runif(10,0,1), rep(c("none","summer","winter","sping","allyear"),2)) names(df)<-c("Mother","ID","Wavelength1","Wavelength2","Wavelength3","WaterTreatment") df Mother ID Wavelength1 Wavelength2 Wavelength3 WaterTreatment 1 2 34 0.9143670 0.03077356 0.82859497 none 2 24 75 0.6173382 0.05958151 0.66552338 summer 3 62 77 0

Pandas long to wide

喜欢而已 提交于 2019-12-24 00:45:13
问题 Using pandas, I want to convert a long data frame to wide but the usual pivot method is not as flexible as I need. Here is the long data: raw = { 'sample':[1, 1, 1, 1, 2, 2, 3, 3, 3, 3], 'gene':['G1', 'G2', 'G3', 'G3', 'G1', 'G2', 'G2', 'G2', 'G3', 'G3'], 'type':['HIGH', 'HIGH', 'LOW', 'MED', 'HIGH', 'LOW', 'LOW', 'LOW', 'MED', 'LOW']} df = pd.DataFrame(raw)` which produces gene sample type G1 1 HIGH G2 1 HIGH G3 1 LOW G3 1 MED G1 2 HIGH G2 2 LOW G2 3 LOW G2 3 LOW G3 3 MED G3 3 LOW What I

Reshape data frame from wide to long with re-occuring column names in R

。_饼干妹妹 提交于 2019-12-22 09:31:50
问题 I'm trying to convert a data frame from wide to long format using the melt formula. The challenge is that I have multiple column names that are labeled the same. When I use the melt function, it drops the values from the repeat column. I have read similar questions and it was advised to use the reshape function, however I was not able to get it work. To reproduce my starting data frame: conversion.id<-c("1", "2", "3") interaction.num<-c("1","1","1") interaction.num2<-c("2","2","2") conversion

Pandas Melt with Multiple Value Vars

萝らか妹 提交于 2019-12-18 13:38:51
问题 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 var1 20 23 25 29 31 32 3 Brazil var2 0 1 2 2 3 3 I want to reshape my data to long so that year, var1, and var2 become new columns Index Country year var1 var2 0 Argentina 2000 12 1 1 Argentina 2001 15 3 2 Argentina 2002 18 2 .... 6 Brazil 2000 20 0 7 Brazil 2001 23 1 I got my code to work when I only had one variable

R cannot melt data.frame

好久不见. 提交于 2019-12-18 13:24:11
问题 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 717.8705 7 521 697 376.734 693.8541 8 529 855 455.7259 3777.856 9 531 855 455.7259 3777.856 10 609 774 278.291 717.8705 when I try to use the function melt() MeltTable <- melt(tableMS,id=c("X","Y")) I get the following error: Error in match.names(clabs, names(xi)) : names do not match