melt

Reshape wide format, to multi-column long format

淺唱寂寞╮ 提交于 2019-11-28 21:28:42
I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 12 13 27 1 2 2008 22 22 24 1 2 2009 10 14 20 2 1 2008 12 13 25 2 1 2009 16 14 21 2 2 2008 13 11 29 2 2 2009 23 20 26 3 1 2008 11 12 22 3 1 2009 13 11 27 3 2 2008 17 12 23 3 2 2009 14 9 31 into a data set that separates the tests by column but converts the measurement time into long format, for each of the new columns like this: ID Year Time Test1 Test2 1 2008 Fall 15 22 1 2008 Spring 16 22 1 2008 Winter 19 24 1 2009 Fall 12 10 1 2009

Melt the Upper Triangular Matrix of a Pandas Dataframe

♀尐吖头ヾ 提交于 2019-11-28 20:25:32
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 I melt only the upper triangle to get Row Column Value a a 1 a b .5 a c .3 b b 1 b c .4 c c 1 #Note the combination a,b is only listed once. There is no b,a listing I'm more interested in an idiomatic pandas solution, a custom indexer would be easy enough to write by hand... Thank you in advance for your consideration and response. First I convert lower values of df to NaN by where and numpy.triu and then stack , reset_index and set column names: import numpy as np print df a b c a 1.0 0.5 0.3 b

reshape2 melt warning message

你说的曾经没有我的故事 提交于 2019-11-28 16:58:33
问题 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 not the case with my dataset. Here is the dataset: test <- structure(list(park = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("miss", "piro", "sacn", "slbe"), class = "factor"), a1.one = structure(c(3L, 1L, 3L, 3L, 3L, 3L, 1L, 3L,

Reshape data for values in one column

China☆狼群 提交于 2019-11-28 12:56:32
My data.frame looks like this ID | test | test_result 1 | B | 10 2 | A | 9 3 | A | 11 4 | C | 7 5 | F | 5 And I want to get something like this: test | test_reult_ID1 | test_result_ID2 | test_result_ID3 ... A | NA | 9 | 11 B | 10 | NA | NA It works with reshape() to the wide format with only a few cases but with the whole data frame (about 23.000 ID´s) reshape () takes too long. Melt() and cast() do reshape the data but replace the values in test_result by the frequency of the test. Any other ideas how to manage this? Thanks! Alex dcast from the reshape2 package does this: require(reshape2)

How to melt R data.frame and plot group by bar plot

我与影子孤独终老i 提交于 2019-11-28 11:35:35
I have following R data.frame: group match unmatch unmatch_active match_active 1 A 10 4 0 0 2 B 116 20 0 3 3 c 160 27 1 4 4 D 79 17 0 3 5 E 309 84 4 14 6 F 643 244 10 23 ... My goal is to plot a group by bar plot ( http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_(ggplot2) / section-Graphs with more variables) as shown in the link. I realize that before getting to that I need to get the data in to following format group variable value 1 A match 10 2 B match 116 3 C match 160 4 D match 79 5 E match 309 6 F match 643 7 A unmatch 4 8 B unmatch 20 ... I used the melt function: groups.df.melt <

Match Dataframes Excluding Last Non-NA Value and disregarding order

泄露秘密 提交于 2019-11-28 10:37:31
I have two dataframes: Partner<-c("Alpha","Beta","Zeta") COL1<-c("A","C","M") COL2<-c("B","D","K") COL3<-c("C","F",NA) COL4<-c("D",NA,NA) df1<-data.frame(Partner,COL1,COL2,COL3,COL4) lift<-c(9,10,11,12,12,23,12,24) RULE1<-c("B","B","D","A","C","K","M","K") RULE2<-c("A","A","C","B","A","M","T","M") RULE3<-c("G","D","M","C" ,"M", "E",NA,NA) RULE4<-c(NA,NA,"K","D" ,NA, NA,NA,NA) df2<-data.frame(lift,RULE1,RULE2,RULE3,RULE4) df1 Partner COL1 COL2 COL3 COL4 Alpha A B C D Beta C D F NA Zeta M K NA NA df2 lift RULE1 RULE2 RULE3 RULE4 9 B A G NA 10 B A D NA 11 D C M K 12 A B C D 12 C A M NA 23 K M E

Using melt with matrix or data.frame gives different output

不想你离开。 提交于 2019-11-28 00:37:30
问题 Consider the following code: set.seed(1) M = matrix(rnorm(9), ncol = 3) dimnames(M) = list(LETTERS[1:3], LETTERS[1:3]) print(M) A B C A -0.6264538 1.5952808 0.4874291 B 0.1836433 0.3295078 0.7383247 C -0.8356286 -0.8204684 0.5757814 melt(M) Var1 Var2 value 1 A A -0.6264538 2 B A 0.1836433 3 C A -0.8356286 4 A B 1.5952808 5 B B 0.3295078 6 C B -0.8204684 7 A C 0.4874291 8 B C 0.7383247 9 C C 0.5757814 If i call melt using a data.frame , i get a different result: DF = data.frame(M) melt(DF)

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

风格不统一 提交于 2019-11-27 15:00:00
问题 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 variables into seperate columns. However I need to do this explicitly by column name, rather than by index location. import pandas as pd df = pd.DataFrame([('a','b','c',1,2,3,'aa','bb','cc'), ('d', 'e', 'f', 4, 5, 6, 'dd', 'ee', 'ff')], columns=['a_1',

Reshape wide format, to multi-column long format

只谈情不闲聊 提交于 2019-11-27 13:51:10
问题 I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 12 13 27 1 2 2008 22 22 24 1 2 2009 10 14 20 2 1 2008 12 13 25 2 1 2009 16 14 21 2 2 2008 13 11 29 2 2 2009 23 20 26 3 1 2008 11 12 22 3 1 2009 13 11 27 3 2 2008 17 12 23 3 2 2009 14 9 31 into a data set that separates the tests by column but converts the measurement time into long format, for each of the new columns like this: ID Year

Melt the Upper Triangular Matrix of a Pandas Dataframe

风流意气都作罢 提交于 2019-11-27 12:16:12
问题 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 I melt only the upper triangle to get Row Column Value a a 1 a b .5 a c .3 b b 1 b c .4 c c 1 #Note the combination a,b is only listed once. There is no b,a listing I'm more interested in an idiomatic pandas solution, a custom indexer would be easy enough to write by hand... Thank you in advance for your consideration and response. 回答1: First I convert lower values of df to NaN by where and