reshape

How to rearrange table in pandas in a format suitable for analysis in R?

我们两清 提交于 2020-01-05 05:52:09
问题 In pandas: df = pd.DataFrame({'row1':['a','b','a','a','b','b','a','b','b','a'], 'row2':['x','x','y','y','y','x','x','y','x','y'],'col':[1,2,1,2,2,1,2,1,1,2],'val':[34,25,22,53,33,19,42,38,33,61]}) p = pd.pivot_table(df,values='val',rows=['row1','row2'],cols='col') col 1 2 row1 row2 a x 34 42 y 22 57 b x 26 25 y 38 33 Is it possible to reshape the pivot table in a data frame suitable for analysis in R?, something of the type: row1 row2 col val a x 1 34 a y 1 22 a x 2 42 a y 2 57 b x 1 26 b y 1

expand mid year values to month in pandas

て烟熏妆下的殇ゞ 提交于 2020-01-05 04:33:06
问题 following from expand year values to month in pandas I have: pd.DataFrame({'comp':['a','b'], 'period':['20180331','20171231'],'value':[12,24]}) comp period value 0 a 20180331 12 1 b 20171231 24 and would like to extrapolate to 201701 to 201812 inclusive. The value should be spread out for the 12 months preceding the period. comp yyymm value a 201701 na a 201702 na ... a 201705 12 a 201706 12 ... a 201803 12 a 201804 na b 201701 24 ... b 201712 24 b 201801 na ... 回答1: Use: #create month

R: Having trouble with reshape() function in stats package

删除回忆录丶 提交于 2020-01-04 21:40:27
问题 When there are multiple variables in a data.frame that need to be melted, I'm confused about how to make that work. Here's an example: Data <- data.frame(SampleID = rep(1:10, each = 3), TimePoint = rep(LETTERS[1:3], 10)) Data$File.ESIpos <- paste("20141031 Subject", Data$SampleID, "Point", Data$TimePoint, "ESIpos") Data$Date.ESIpos <- "20141031" Data$File.ESIneg <- paste("20141030 Subject", Data$SampleID, "Point", Data$TimePoint, "ESIneg") Data$Date.ESIneg <- "20141030" Data$File.APCIpos <-

How to reshape a multidimensional array to a 2D image?

落花浮王杯 提交于 2020-01-04 09:05:21
问题 I'm working on an array shaped as follows (64, 1, 64, 64) This is in fact one grayscale image that was split into 64 patches, each patch with 64*64px. Now I need to rebuild it into a 512*512px image. I've tried using np.reshape(arr, (512, 512)) but of course the resulting image is not as expected. How do I resolve this? 回答1: It depends on how your patches are arranged. But the first thing you could try is image.reshape(8, 8, 64, 64).swapaxes(1, 2).reshape(512, 512) This is assuming that the

Can pandas split/merge columns based on patterns in their name?

帅比萌擦擦* 提交于 2020-01-04 00:14:29
问题 Can pandas split and/or merge columns, based on patterns in the column name? Here's a DataFrame : meas1_left meas1_right meas2_left meas2_right 0 1 2 3 4 1 6 7 8 9 I'd like to turn the above data and this (I don't really care how the new frame is indexed): meas1 meas2 side 0 1 3 left 1 2 4 right 2 6 8 left 3 7 9 right 回答1: You can first create Multiindex from columns by split: df.columns = df.columns.str.split('_', expand=True) print (df) meas1 meas2 left right left right 0 1 2 3 4 1 6 7 8 9

Data manipulation with forecasting data

烂漫一生 提交于 2020-01-03 04:33:49
问题 I am trying to make forecasting for sales from two stores: Store 1 and Store 2. Like results from forecasting with forecasting package I got this two table.First table contain data about MAPE error separably by each model(column Value).Below you can see data and screen shot of data. Table_1<-structure(list(...1 = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"), X1 = c("SNAIVE", "HW", "ETS", "ARIMA", "STL", "TBATS", "NNETAR",

R + reshape : variance of columns of a data.frame

前提是你 提交于 2020-01-03 02:29:10
问题 I'm using reshape in R to compute aggregate statistics over columns of a data.frame. Here's my data.frame: > df a a b b ID 1 1 1 1 1 1 2 2 3 2 3 2 3 3 5 3 5 3 which is just a little test data.frame to try and understand the reshape package. I melt, and then cast, to try and find the mean of the a s and the b s: > melt(df, id = "ID") -> df.m > cast(df.m, ID ~ variable, fun = mean) ID a b 1 1 1 1 2 2 2 2 3 3 3 3 Argh! What? Was hoping the mean of c(2,3) was 2.5 and so on. What's going on? Here

with c_f_pointer is fortran array reshape in-place or not

时光毁灭记忆、已成空白 提交于 2020-01-02 09:11:56
问题 I have a question related to one asked some years ago on Intel Developer Forum about the in-place reshaping of an array. In short, the answer was that an array of a certain rank can be allocated, and a pointer created that refers to the same memory location (i.e. in-place), but with a different rank, e.g.: use, intrinsic :: ISO_C_BINDING integer, allocatable, target :: rank1_array(:) integer, pointer :: rank3_array(:,:,:) integer :: i ! Allocate rank1_array allocate(rank1_array(24)) ! Created

Reshape pandas dataframe from rows to columns

亡梦爱人 提交于 2020-01-02 02:22:07
问题 I'm trying to reshape my data. At first glance, it sounds like a transpose, but it's not. I tried melts, stack/unstack, joins, etc. Use Case I want to have only one row per unique individual, and put all job history on the columns. For clients, it can be easier to read information across rows rather than reading through columns. Here's the data: import pandas as pd import numpy as np data1 = {'Name': ["Joe", "Joe", "Joe","Jane","Jane"], 'Job': ["Analyst","Manager","Director","Analyst",

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