reshape

How to use acast (reshape2) within a function in R?

浪尽此生 提交于 2019-12-20 23:19:06
问题 I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it. Here is my data: library("reshape2") x <- data.frame(1:3, rnorm(3), rnorm(3), rnorm(3)) colnames(x) <- c("id", "var1", "var2", "var3") y <-melt(x, id = "id", measure = c("var1", "var2", "var3")) y then looks like this: id variable value 1 1 var1 0.1560812 2 2 var1 1.0343844 3 3 var1 -1.4157728 4 1 var2 0.8808935 5 2 var2 0.1719239 6 3 var2 0.6723758 7 1 var3 -0

AttributeError: 'Series' object has no attribute 'reshape'

拜拜、爱过 提交于 2019-12-20 17:41:24
问题 I'm using sci-kit learn linear regression algorithm. While scaling Y target feature with: Ys = scaler.fit_transform(Y) I got ValueError: Expected 2D array, got 1D array instead: After that I reshaped using: Ys = scaler.fit_transform(Y.reshape(-1,1)) But got error again: AttributeError: 'Series' object has no attribute 'reshape' So I checked pandas.Series documentation page and it says: reshape(*args, **kwargs) Deprecated since version 0.19.0. 回答1: Solution was linked on reshaped method on

Converting a numeric matrix into a data.table (or data.frame)

别等时光非礼了梦想. 提交于 2019-12-20 12:38:36
问题 Hoping there's a simple answer here but I can't find it anywhere. I have a numeric matrix with labelled rows and columns: 1 2 3 4 a 6 7 8 9 b 8 7 5 7 c 8 5 4 1 d 1 6 3 2 I would like a data.table (or a data.frame I can then convert) of the form: col row value 1 a 6 1 b 8 1 c 8 1 d 1 2 a 7 2 b 7 2 c 5 2 d 6 ... Any tips appreciated. 回答1: Use melt from reshape2: library(reshape2) #Fake data x <- matrix(1:12, ncol = 3) colnames(x) <- letters[1:3] rownames(x) <- 1:4 x.m <- melt(x) x.m Var1 Var2

pandas pivot_table column names

大兔子大兔子 提交于 2019-12-20 12:27:02
问题 For a dataframe like this: d = {'id': [1,1,1,2,2], 'Month':[1,2,3,1,3],'Value':[12,23,15,45,34], 'Cost':[124,214,1234,1324,234]} df = pd.DataFrame(d) Cost Month Value id 0 124 1 12 1 1 214 2 23 1 2 1234 3 15 1 3 1324 1 45 2 4 234 3 34 2 to which I apply pivot_table df2 = pd.pivot_table(df, values=['Value','Cost'], index=['id'], columns=['Month'], aggfunc=np.sum, fill_value=0) to get df2: Cost Value Month 1 2 3 1 2 3 id 1 124 214 1234 12 23 15 2 1324 0 234 45 0 34 is there an easy way to

Converting data from wide to long (using multiple columns) [duplicate]

妖精的绣舞 提交于 2019-12-20 07:29:06
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed last year . I currently have wide data which looks similar to this: cid dyad f1 f2 op1 op2 ed1 ed2 junk 1 2 0 0 2 4 5 7 0.876 1 5 0 1 2 4 4 3 0.765 etc And I wish into a long data frame which looks similar to this: cid dyad f op ed junk id 1 2 0 2 5 0.876 1 1 2 0 4 7 0.876 2 1 5 0 2 4 0.765 1 1 5 1 4 3 0.765 2 I have tried using the gather(

reshape scipy csr matrix

為{幸葍}努か 提交于 2019-12-20 07:16:09
问题 How can I reshape efficiently and scipy.sparse csr_matrix? I need to add zero rows at the end. Using: from scipy.sparse import csr_matrix data = [1,2,3,4,5,6] col = [0,0,0,1,1,1] row = [0,1,2,0,1,2] a = csr_matrix((data, (row, col))) a.reshape(3,5) I get this error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.5/dist-packages/scipy/sparse/base.py", line 129, in reshape self.__class__.__name__) NotImplementedError: Reshaping not

Reshaping a column from a data frame into several columns using R [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-20 06:39:20
问题 This question already has answers here : Faster ways to calculate frequencies and cast from long to wide (4 answers) Closed last year . I have a database that looks like this: start<-as.POSIXct("2012-01-15") interval<-60 end<-start+as.difftime(31,units="days") date<-seq(from=start,by=interval*60, to=end) # date/time information l<-length(date) stations<-as.factor(rep(1:3,len=l)) # stations df<-data.frame(date,stations) # data frame What I would like is to reshape the station column from this

R Partial Reshape Data from Long to Wide

主宰稳场 提交于 2019-12-20 05:40:43
问题 I like to reshape a dataset from long to wide. Specifically, the new wide dataset should consist of rows corresponding to the unique number of IDs in the long dataset, and the number of columns is a multiple of unique values of another variable. Let's say this is the original dataset: ID a b C d e f g 1 1 1 1 1 2 3 4 1 1 1 2 5 6 7 8 2 2 2 1 1 2 3 4 2 2 2 3 9 0 1 2 2 2 2 2 5 6 7 8 3 3 3 3 9 0 1 2 3 3 3 2 5 6 7 8 3 3 3 1 1 2 3 4 In the new dataset, the number of rows is the number of IDs, the

Reshape messy longitudinal survey data containing multiple different variables, wide to long

天涯浪子 提交于 2019-12-20 05:31:36
问题 I hope that I'm not recreating the wheel, and do not think that the following can be answered using reshape . I have messy longitudinal survey data, that I want to convert from wide to long format. By messy I mean: I have a mixture of variable types (numeric, factor, logical) Not all variables have been collected at every timepoint. For example: data <- read.table(header=T, text=' id inlove.1 inlove.2 income.2 income.3 mood.1 mood.3 random 1 TRUE FALSE 87717.76 82281.25 happy happy filler 2

How to reshape dataframe and transpose recurring columns to dataframe rows?

╄→гoц情女王★ 提交于 2019-12-20 05:12:08
问题 I have a dataframe that has recurring columns (the interval is 5). my dataframe at the moment So this is how it looks: I have 5 type of columns and they repeat time over time. The recurring columns have a suffix in their name, this can be removed/renamed as well, so that they would all match. What I would like to do is to transpose these recurring columns to rows, so that I would have only 5 columns in the end (Dates, PX_LAST, PX_HIGH, PX_VOLUME, Name). Then I would be able to group the