AttributeError: ‘Series’ object has no attribute ‘reshape’
Solution was linked on reshaped method on documentation page. Insted of Y.reshape(-1,1) you need to use: Y.values.reshape(-1,1)
Solution was linked on reshaped method on documentation page. Insted of Y.reshape(-1,1) you need to use: Y.values.reshape(-1,1)
Pure numpy numpy.loadtxt(open(“test.csv”, “rb”), delimiter=”,”, skiprows=1) Check out the loadtxt documentation. You can also use python’s csv module: import csv import numpy reader = csv.reader(open(“test.csv”, “rb”), delimiter=”,”) x = list(reader) result = numpy.array(x).astype(“float”) You will have to convert it to your favorite numeric type. I guess you can write the whole thing in one line: … Read more
Here’s another solution more fleshed out, taken from Chris Albon’s site. Create “long” dataframe raw_data = {‘patient’: [1, 1, 1, 2, 2], ‘obs’: [1, 2, 3, 1, 2], ‘treatment’: [0, 1, 0, 1, 0], ‘score’: [6252, 24243, 2345, 2342, 23525]} df = pd.DataFrame(raw_data, columns = [‘patient’, ‘obs’, ‘treatment’, ‘score’]) Make a “wide” data df.pivot(index=’patient’, columns=”obs”, … Read more
there are a few ways; using .pivot: >>> origin.pivot(index=’label’, columns=”type”)[‘value’] type a b c label x 1 2 3 y 4 5 6 z 7 8 9 [3 rows x 3 columns] using pivot_table: >>> origin.pivot_table(values=”value”, index=’label’, columns=”type”) value type a b c label x 1 2 3 y 4 5 6 z 7 8 … Read more
This approach seems pretty natural to me: df %>% gather(key, value, -id, -time) %>% extract(key, c(“question”, “loop_number”), “(Q.\\..)\\.(.)”) %>% spread(question, value) First gather all question columns, use extract() to separate into question and loop_number, then spread() question back into the columns. #> id time loop_number Q3.2 Q3.3 #> 1 1 2009-01-01 1 0.142259203 -0.35842736 #> … Read more
There are many ways to do this. This answer starts with what is quickly becoming the standard method, but also includes older methods and various other methods from answers to similar questions scattered around this site. tmp <- data.frame(x=gl(2,3, labels=letters[24:25]), y=gl(3,1,6, labels=letters[1:3]), z=c(1,2,3,3,3,2)) Using the tidyverse: The new cool new way to do this is … Read more
As of Dec 2014, this can be done using the unnest function from Hadley Wickham’s tidyr package (see release notes http://blog.rstudio.org/2014/12/08/tidyr-0-2-0/) > library(tidyr) > library(dplyr) > mydf V1 V2 2 1 a,b,c 3 2 a,c 4 3 b,d 5 4 e,f 6 . . > mydf %>% mutate(V2 = strsplit(as.character(V2), “,”)) %>% unnest(V2) V1 V2 … Read more
Three alternative solutions: 1) With data.table: You can use the same melt function as in the reshape2 package (which is an extended & improved implementation). melt from data.table has also more parameters that the melt-function from reshape2. You can for example also specify the name of the variable-column: library(data.table) long <- melt(setDT(wide), id.vars = c(“Code”,”Country”), … Read more
Using reshape function: reshape(dat1, idvar = “name”, timevar = “numbers”, direction = “wide”)
The criterion to satisfy for providing the new shape is that ‘The new shape should be compatible with the original shape’ numpy allow us to give one of new shape parameter as -1 (eg: (2,-1) or (-1,3) but not (-1, -1)). It simply means that it is an unknown dimension and we want numpy to … Read more