melt

melt column by substring of the columns name in pandas (python)

ⅰ亾dé卋堺 提交于 2020-01-24 12:52:12
问题 I have dataframe: subject A_target_word_gd A_target_word_fd B_target_word_gd B_target_word_fd subject_type 1 1 2 3 4 mild 2 11 12 13 14 moderate And I want to melt it to a dataframe that will look: cond subject subject_type value_type value A 1 mild gd 1 A 1 mild fg 2 B 1 mild gd 3 B 1 mild fg 4 A 2 moderate gd 11 A 2 moderate fg 12 B 2 moderate gd 13 B 2 moderate fg 14 ... ... Meaning, to melt based on the delimiter of the columns name. What is the best way to do that? 回答1: One more approach

wide to long multiple columns issue

佐手、 提交于 2020-01-16 09:48:16
问题 I have something like this: id role1 Approved by Role1 role2 Approved by Role2 1 Amy 1/1/2019 David 4/4/2019 2 Bob 2/2/2019 Sara 5/5/2019 3 Adam 3/3/2019 Rachel 6/6/2019 I want something like this: id Name Role Approved 1 Amy role1 1/1/2019 2 Bob role1 2/2/2019 3 Adam role1 3/3/2019 1 David role2 4/4/2019 2 Sara role2 5/5/2019 3 Rachel role2 6/6/2019 I thought something like this would work melt(df,id.vars= id, measure.vars= list(c("role1", "role2"),c("Approved by Role1", "Approved by Role2")

Reshape package masking preventing melt from naming columns

谁说胖子不能爱 提交于 2020-01-12 22:30:50
问题 I have a script which requires both reshape and reshape2 libraries. I know this is poor practise, but I think plyr (or another library I am using) Vennerable is loading reshape and I have personally used reshape2 in a lot of places. The problem is that the masking of reshape2 by reshape is causing problems for the melt function # Example data frame df <- data.frame(id=c(1:5), a=c(rnorm(5)), b=c(rnorm(5))) # With just reshape2, variable and value columns are labelled correctly library(reshape2

R: Melt and Dcast

╄→гoц情女王★ 提交于 2020-01-11 11:28:30
问题 I have a dataset like this: CASE_ID = c("C1","C1", "C2","C2", "C2", "C3", "C4") PERSON_ID = c(1,0,7,8,1,20,7) PERSON_DIVISION = c("Zone 1", "NA", "Zone 1", "Zone 3", "Zone 1", "Zone 5", "Zone 1") df <- data.frame(CASE_ID, PERSON_ID, PERSON_DIVISION) df That results in: CASE_ID PERSON_ID PERSON_DIVISION 1 C1 1 Zone 1 2 C1 0 NA 3 C2 7 Zone 1 4 C2 8 Zone 3 5 C2 1 Zone 1 6 C3 20 Zone 5 7 C4 7 Zone 1 And I want to transform it in: CASE_ID P1_ID P2_ID P3_ID P1_Division P2_Division P3_Division 1 1 0

How to getRange in all cell appear when reshape data for wide to long form by monthly columns name GooglespreadSheet

╄→尐↘猪︶ㄣ 提交于 2020-01-07 09:25:16
问题 In R , data.table library dcast() can transform dataset from wide to long shape ,how can i do this in googlespreadsheet? Sheet1 Name Type YTD JAN FEB MAR Product 1 A 8 1 3 4 Product 2 B 519 41 23 455 Product 3 C 32 2 25 5 NA D 3 NA 2 1 Sheet2 A B C D E F 1 Name Type YTD JAN FEB MAR 2 =filter(Sheet1!A2:F5,not(isblank(Sheet1!A2:A5))) Show reshaped data in Sheet3 from A1 [ ** C column for YTD is not necessarily needed . Adjusted script by me not works : from Tanaike function myFunction() { var

How to melt and plot multiple datasets over different ranges on the same set of axis?

梦想与她 提交于 2020-01-06 05:45:10
问题 This is my first time posting here, I hope my question is clear and appropriate. I have a set of data the head of which looks like this: wl ex421 wl ex309 wl ex284 wl ex347 1 431 0.6168224 321 0.1267943 301 0.06392694 361 0.15220484 2 432 0.6687435 322 0.2416268 302 0.05631659 362 0.08961593 3 433 0.6583593 323 0.4665072 303 0.05327245 363 0.13134187 4 434 0.6832814 324 0.3576555 304 0.00000000 364 0.32432432 5 435 0.6427830 325 0.2194976 305 0.12328767 365 0.50308203 6 436 0.7393562 326 0

Melting/Splitting a row into two rows, using two column values in the original row, leaving the rest intact

心不动则不痛 提交于 2020-01-05 03:48:06
问题 I have a data.table as follows: DT <- fread( "ID country year Event_A Event_B 4 NLD 2002 0 1 5 NLD 2002 0 1 6 NLD 2006 1 1 7 NLD 2006 1 0 8 NLD 2006 1 1 9 GBR 2002 0 1 10 GBR 2002 0 0 11 GBR 2002 0 1 12 GBR 2006 1 1 13 GBR 2006 1 1", header = TRUE) I want to cast the event columns over the row without summing them, creating new rows. I tried: meltedsessions <- melt(Exp, id.vars = -c(Event_A", "Event_B"), measure.vars = c("Event_A", "Event_B")) I need to specify id.vars as a negative because

How to strsplit data frame column and replicate rows accordingly? [duplicate]

老子叫甜甜 提交于 2020-01-03 10:19:08
问题 This question already has answers here : Split comma-separated strings in a column into separate rows (5 answers) Closed 3 years ago . I have a data frame like this: > df <- data.frame(Column1=c("id1", "id2", "id3"), Column2=c("text1,text2,text3", "text4", "text5,text6"), Column3=c("text7", "text8,text9,text10,text11", "text12,text13")) > df Column1 Column2 Column3 1 id1 text1,text2,text3 text7 2 id2 text4 text8,text9,text10,text11 3 id3 text5,text6 text12,text13 How do I transform it in this

Count Number of Rows GroupBy within a GroupBy Between Two Dates in Pandas Dataframe

落爺英雄遲暮 提交于 2020-01-03 02:20:11
问题 I have a dataframe df , which can be created with the following code: import random from datetime import timedelta import pandas as pd import datetime #create test range of dates rng=pd.date_range(datetime.date(2015,7,15),datetime.date(2015,7,31)) rnglist=rng.tolist() testpts = range(100,121) #create test dataframe d={'jid':[i for i in range(100,121)], 'cid':[random.randint(1,2) for _ in testpts], 'ctid':[random.randint(3,4) for _ in testpts], 'stdt':[rnglist[random.randint(0,len(rng))] for _

Want to cast unique values into first/second/third variables

扶醉桌前 提交于 2020-01-01 19:18:11
问题 I have a sample of a dataset that needs to be cast into a wide format, but I have a particular issue that I haven't seen addressed on StackOveflow yet. The column that I'd like to use to make a long dataset has unique values for every single row, but I want to create a new dataset so that are n variables for n attributes for each idvar. I need to convert this: state sector attribute_value alabama 1 a alabama 1 b alabama 1 c alabama 1 d alabama 1 e alabama 1 f alabama 1 g alabama 1 h alaska 1