Questions tagged [reshape]

In R, Matlab, NumPy and APL, reshape functions allow data to be transformed into more convenient forms.

Reshape functions allow data to be transformed into more convenient forms.

R

The function reshapes a data frame between ‘wide’ format with repeated measurements in separate columns of the same record and ‘long’ format with the repeated measurements in separate records.

Matlab

The function allows a vector or array to be transformed into a new array with the specified dimensions.
Note that reshape does not change the order of the elements or the number of elements in the array. reshape only affects its shape.

NumPy

The function gives a new shape to an array without changing its data. The returned array will be a new view object if possible; otherwise, it will be a copy.

APL

The function allows any array to be transformed into a new array with the specified shape. Note that does not change the order of the elements, however it can change the number of elements in the array, recycling elements if they are insufficient to fill the requested shape, or truncating trailing elements if the requested shape cannot hold them all.

3858 questions
661
votes
12 answers

What does -1 mean in numpy reshape?

A 2D array can be reshaped into a 1D array using .reshape(-1). For example: >>> a = numpy.array([[1, 2, 3, 4], [5, 6, 7, 8]]) >>> a.reshape(-1) array([[1, 2, 3, 4, 5, 6, 7, 8]]) Usually, array[-1] means the last element. But what does -1 mean here?
user2262504
  • 7,057
  • 5
  • 18
  • 23
366
votes
14 answers

How to reshape data from long to wide format

I'm having trouble rearranging the following data frame: set.seed(45) dat1 <- data.frame( name = rep(c("firstName", "secondName"), each=4), numbers = rep(1:4, 2), value = rnorm(8) ) dat1 name numbers value 1 firstName …
Steve
  • 5,727
  • 10
  • 32
  • 30
248
votes
8 answers

Reshaping data.frame from wide to long format

I have some trouble to convert my data.frame from a wide table to a long table. At the moment it looks like this: Code Country 1950 1951 1952 1953 1954 AFG Afghanistan 20,249 21,352 22,532 23,557 24,555 ALB Albania …
mropa
  • 11,562
  • 10
  • 33
  • 29
158
votes
6 answers

Split delimited strings in a column and insert as new rows

I have a data frame as follow: +-----+-------+ | V1 | V2 | +-----+-------+ | 1 | a,b,c | | 2 | a,c | | 3 | b,d | | 4 | e,f | | . | . | +-----+-------+ Each of the alphabet is a character separated by comma. I would like to…
Boxuan
  • 4,937
  • 6
  • 37
  • 73
147
votes
6 answers

Reshape three column data frame to matrix ("long" to "wide" format)

I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: a b c x 1 2 3 y 3 3 2 I…
MalteseUnderdog
  • 1,971
  • 5
  • 17
  • 17
116
votes
5 answers

Gather multiple sets of columns

I have data from an online survey where respondents go through a loop of questions 1-3 times. The survey software (Qualtrics) records this data in multiple columns—that is, Q3.2 in the survey will have columns Q3.2.1., Q3.2.2., and Q3.2.3.: df <-…
Andrew
  • 36,541
  • 13
  • 67
  • 93
107
votes
6 answers

Pandas long to wide reshape, by two variables

I have data in long format and am trying to reshape to wide, but there doesn't seem to be a straightforward way to do this using melt/stack/unstack: Salesman Height product price Knut 6 bat 5 Knut 6 ball …
Luke
  • 6,699
  • 13
  • 50
  • 88
103
votes
3 answers

Opposite of melt in python pandas

I cannot figure out how to do "reverse melt" using Pandas in python. This is my starting data 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 …
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
87
votes
3 answers

load csv into 2D matrix with numpy for plotting

Given this CSV…
dgorissen
  • 6,207
  • 3
  • 43
  • 52
81
votes
4 answers

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

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 =…
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
77
votes
7 answers

Subsetting R data frame results in mysterious NA rows

I've been encountering what I think is a bug. It's not a big deal, but I'm curious if anyone else has seen this. Unfortunately, my data is confidential, so I have to make up an example, and it's not going to be very helpful. When subsetting my…
chrisg
  • 801
  • 2
  • 8
  • 5
75
votes
11 answers

PyTorch reshape tensor dimension

I want to reshape a vector of shape (5,) into a matrix of shape (1, 5). With numpy, I can do: >>> import numpy as np >>> a = np.array([1, 2, 3, 4, 5]) >>> a.shape (5,) >>> a = np.reshape(a, (1, 5)) >>> a.shape (1, 5) >>> a array([[1, 2, 3, 4,…
Haha TTpro
  • 5,137
  • 6
  • 45
  • 71
65
votes
4 answers

Rearrange dataframe to a table, the opposite of "melt"

I have huge dataframe like this: SN = c(1:100, 1:100, 1:100, 1:100) class = c(rep("A1", 100), rep("B2", 100), rep("C3", 100), rep("D4", 100)) # total 6000 levels myvar = rnorm(400) mydf = data.frame(SN, class, myvar) I want to "unmelt" to a…
jon
  • 11,186
  • 19
  • 80
  • 132
58
votes
3 answers

Melt the Upper Triangular Matrix of a Pandas Dataframe

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 the upper triangle be melted to get a matrix of the following form Row Column Value a a 1 a b .5 a …
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125
57
votes
2 answers

Grouped bar plot in ggplot

I have a survey file in which row are observation and column question. Here are some fake data they look like: People,Food,Music,People P1,Very Bad,Bad,Good P2,Good,Good,Very Bad P3,Good,Bad,Good P4,Good,Very Bad,Very Good P5,Bad,Good,Very…
S12000
  • 3,345
  • 12
  • 35
  • 51
1
2 3
99 100