melt

Opposite of melt in python pandas

假装没事ソ 提交于 2019-11-27 10:52:09
I cannot figure out how to do "reverse melt" using Pandas in python. This is my starting data import pandas as pd from StringIO import StringIO origin = pd.read_table(StringIO('''label type value x a 1 x b 2 x c 3 y a 4 y b 5 y c 6 z a 7 z b 8 z c 9''')) origin Out[5]: 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 7 7 z b 8 8 z c 9 This is the output I would like to have: label a b c x 1 2 3 y 4 5 6 z 7 8 9 I'm sure there is an easy way to do this, but I don't know how. there are a few ways; using .pivot : >>> origin.pivot(index='label', columns='type')['value'] type a

Pandas DataFrame stack multiple column values into single column

元气小坏坏 提交于 2019-11-27 08:42:39
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. Alexander You can melt your dataframe: >>> keys = [c for c in df if c.startswith('key.')] >>> pd.melt(df, id_vars='topic', value_vars=keys, value_name='key') topic variable key 0 8 key.0 abc 1 9 key

R: Pivoting using 'spread' function

牧云@^-^@ 提交于 2019-11-27 07:30:00
问题 Continuing from my previous post, I am now having 1 more column of ID values that I need to use to pivot rows into columns. NUM <- c(1,2,3,1,2,3,1,2,3,1) ID <- c("DJ45","DJ45","DJ45","DJ46","DJ46","DJ46","DJ47","DJ47","DJ47","DJ48") Type <- c("A", "F", "C", "B", "D", "A", "E", "C", "F", "D") Points <- c(9.2,60.8,22.9,1012.7,18.7,11.1,67.2,63.1,16.7,58.4) df1 <- data.frame(ID,NUM,Type,Points) df1: +------+-----+------+--------+ | ID | Num | Type | Points | +------+-----+------+--------+ | DJ45

Reshape data for values in one column

江枫思渺然 提交于 2019-11-27 07:20:37
问题 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

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

£可爱£侵袭症+ 提交于 2019-11-27 06:22:14
问题 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

Match Dataframes Excluding Last Non-NA Value and disregarding order

有些话、适合烂在心里 提交于 2019-11-27 03:49:42
问题 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

Convert numeric representation of 'variable' column to original string following melt using patterns

旧街凉风 提交于 2019-11-26 22:55:12
I am using the patterns() argument in data.table::melt() to melt data that has columns that have several easily-defined patterns. It is working, but I'm not seeing how I can create a character index variable instead of the default numeric breakdown. For example, in A the dog and cat columns are numbered... take a look at at the "variable" column: A = data.table(idcol = c(1:5), dog_1 = c(1:5), cat_1 = c(101:105), dog_2 = c(6:10), cat_2 = c(106:110), dog_3 = c(11:15), cat_3 = c(111:115)) head(melt(A, measure = patterns("^dog", "^cat"), value.name = c("dog", "cat"))) idcol variable dog cat 1: 1 1

Opposite of melt in python pandas

拈花ヽ惹草 提交于 2019-11-26 15:19:25
问题 I cannot figure out how to do "reverse melt" using Pandas in python. This is my starting data import pandas as pd from StringIO import StringIO origin = pd.read_table(StringIO('''label type value x a 1 x b 2 x c 3 y a 4 y b 5 y c 6 z a 7 z b 8 z c 9''')) origin Out[5]: 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 7 7 z b 8 8 z c 9 This is the output I would like to have: label a b c x 1 2 3 y 4 5 6 z 7 8 9 I'm sure there is an easy way to do this, but I don't know

Reshaping wide to long with multiple values columns [duplicate]

Deadly 提交于 2019-11-26 11:45:52
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers 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 bB C 20 7 20 8 bC D 22 8 22 9 bD ') # Now I want to melt this table, keeping both AVG and SD as separate fields for each measurement, to get something like this: # sbj var avg sd blabla # A f1 10 6 bA # A f2 50 10 bA # B f1 12 5 bB # B f2 70 11

Convert numeric representation of &#39;variable&#39; column to original string following melt using patterns

我的梦境 提交于 2019-11-26 07:44:24
问题 I am using the patterns() argument in data.table::melt() to melt data that has columns that have several easily-defined patterns. It is working, but I\'m not seeing how I can create a character index variable instead of the default numeric breakdown. For example, in A the dog and cat columns are numbered... take a look at at the \"variable\" column: A = data.table(idcol = c(1:5), dog_1 = c(1:5), cat_1 = c(101:105), dog_2 = c(6:10), cat_2 = c(106:110), dog_3 = c(11:15), cat_3 = c(111:115))