reshape

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

六眼飞鱼酱① 提交于 2019-12-20 05:12:07
问题 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

How to reshape () the sum of odd and even rows in numpy

我怕爱的太早我们不能终老 提交于 2019-12-20 03:49:16
问题 Example1 : a = np.array([[[1,11,111],[2,22,222]], [[3,33,333],[4,44,444]], [[5,55,555],[6,66,666]],[[7,77,777],[8,88,888]]]) >>> a array([[[ 1, 11, 111], [ 2, 22, 222]], [[ 3, 33, 333], [ 4, 44, 444]], [[ 5, 55, 555], [ 6, 66, 666]], [[ 7, 77, 777], [ 8, 88, 888]]]) i want reshape() 2D-array and combine odd rows and even rows. Desired result : [[1, 11, 111, 3, 33, 333, 5, 55, 555, 7, 77, 777], [2, 22, 222, 4, 44, 444, 6, 66, 666, 8, 88, 888]] How can I make the output like above? 回答1: Permute

Combine data in many row into a columnn

我是研究僧i 提交于 2019-12-20 03:21:48
问题 I have a data like this: year Male 1 2011 8 2 2011 1 3 2011 4 4 2012 3 5 2012 12 6 2012 9 7 2013 4 8 2013 3 9 2013 3 and I need to group the data for the year 2011 in one column, 2012 in the next column and so on. 2011 2012 2013 1 8 3 4 2 1 12 3 3 4 9 3 How do I achieve this? 回答1: One option is unstack if the number of rows per 'year' is the same unstack(df1, Male ~ year) 回答2: One option is to use functions from dplyr and tidyr . library(dplyr) library(tidyr) dt2 <- dt %>% group_by(year) %>%

reshape an array of images

半腔热情 提交于 2019-12-20 03:12:32
问题 I have 60000 train_images brought in as a shape (28,28,60000) matrix. It is a numpy.ndarray. I want to convert it to an array of 1 dimensional images, meaning each image is represented as a single line/array of numbers, and I want 60000 arrays. In other words, I want to go from (28, 28, 60000) to (60000, 28*28). In python, it would be: images_features = [] for image in images: imageLine = [] for y in range(len(image)): for x in range(len(image[0])): imageLine.append(image[y][x]) images

reshape an array of images

怎甘沉沦 提交于 2019-12-20 03:12:12
问题 I have 60000 train_images brought in as a shape (28,28,60000) matrix. It is a numpy.ndarray. I want to convert it to an array of 1 dimensional images, meaning each image is represented as a single line/array of numbers, and I want 60000 arrays. In other words, I want to go from (28, 28, 60000) to (60000, 28*28). In python, it would be: images_features = [] for image in images: imageLine = [] for y in range(len(image)): for x in range(len(image[0])): imageLine.append(image[y][x]) images

Python pandas pivot from long to wide

时光怂恿深爱的人放手 提交于 2019-12-20 03:11:04
问题 My data is currently in a long format. Below is a sample: Stock Date Time Price Year AAA 2001-01-05 15:20:09 2.380 2001 AAA 2002-02-23 10:13:24 2.440 2002 AAA 2002-02-27 17:17:55 2.460 2002 BBB 2006-05-13 16:03:49 2.780 2006 BBB 2006-10-04 10:33:10 2.800 2006 I would like to reshape it into a wide format by "Stock" and "Year" like so: Stock Year Date1 Time1 Price1 Date2 Time2 Price2 AAA 2001 2001-01-05 15:20:09 2.380 AAA 2002 2002-02-23 10:13:24 2.440 2002-02-27 17:17:55 2.460 BBB 2006 2006

Strsplit on a column of a data frame [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-12-20 02:59:20
问题 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 where one of the variables is a vector (or a list), like this: MyColumn <- c("A, B,C", "D,E", "F","G") MyDF <- data.frame(group_id=1:4, val=11:14, cat=MyColumn) # group_id val cat # 1 1 11 A, B,C # 2 2 12 D,E # 3 3 13 F # 4 4 14 G I'd like to have a new data frame with as many rows as the vector FlatColumn <- unlist(strsplit(MyColumn,split=

Creating cohort-style data frame from set of observations [duplicate]

岁酱吖の 提交于 2019-12-20 01:07:48
问题 This question already has answers here : Transpose / reshape dataframe without “timevar” from long to wide format (6 answers) Closed last year . I'm new to R and have a simple question, as I'm still learning the style of R data manipulation/management. I have a dataset of observations of basic clinical features (blood pressure, cholesterol, etc) over a period of time. Each observation has a patient ID and date, but are entered as separate line items. Something like this: Patient ID Date Blood

Easy assignments with empty square brackets? x[]<-

隐身守侯 提交于 2019-12-19 12:51:15
问题 While looking at an answer posted recently on SO, I noticed an unfamiliar assignment statement. Instead of the usual form of myVar<- myValue , it used the for myVar[]<- myValue . Personally, I had never seen such an assignment, but it had a highly useful effect-- it reshaped the assigned data myValue to the shape of myVar. I would like to use this in my code. However the documentation for "<-" seems to be silent on it. Is this a well established feature and one can rely on it to work in all

Flatten list column in data frame with ID column

∥☆過路亽.° 提交于 2019-12-19 12:29:41
问题 My data frame contains the output of a survey with a select multiple question type. Some cells have multiple values. df <- data.frame(a=1:3,b=I(list(1,1:2,1:3))) df a b 1 1 1 2 2 1, 2 3 3 1, 2, 3 I would like to flatten out the list to obtain the following output: df a b 1 1 1 2 2 1 3 2 2 4 3 1 5 3 2 6 3 3 should be easy but somehow I can't find the search terms. thanks. 回答1: You can just use unnest from "tidyr": library(tidyr) unnest(df, b) # a b # 1 1 1 # 2 2 1 # 3 2 2 # 4 3 1 # 5 3 2 # 6 3