reshape

Python pandas melting data to multiple columns and coulmn names in another column

谁说胖子不能爱 提交于 2019-12-13 16:40:42
问题 I have a dataframe which I want to melt the data into multiple target columns. The below code I used grp2 = pd.lreshape(grp1, cols.groupby(cols.str.split('_').str[1])).sort_values('ACCT_NAME') The above line I lose the column names grp2 = pd.melt(grp1 , id_vars = ['Client' , 'Industry'] , var_name = "H Year" , value_name = 'Count') The above line I dont get multiple target columns From DF Client INDUSTRY 1H2016_6MO 2H2016_6MO 1H2017_6MO 2H2017_6MO 1H2016_12MO 2H2016_12MO 1H2017_12MO 2H2017

Reformat table in R

痴心易碎 提交于 2019-12-13 16:17:45
问题 I have a table as below (different row with same ID will have same gender and age value but different category and sub category value): ID product.category sub.category gender age 1 1 food chicken M young 2 1 kitchen napkin M young 3 1 food steak M young 4 2 electronic phone F mid 5 3 cloth shirt M old 6 3 kitchen bowl M old 7 4 alch beer F young And by combine different rows with same ID, I want to reform the table as below: ID product.category1 sub.category1 product.category2 sub.category2

Python reshape list to ndim array

我与影子孤独终老i 提交于 2019-12-13 11:57:05
问题 Hi I have a list flat which is length 2800, it contains 100 results for each of 28 variables: Below is an example of 4 results for 2 variables [0, 0, 1, 1, 2, 2, 3, 3] I would like to reshape the list to an array (2,4) so that the results for each variable are in a single element. [[0,1,2,3], [0,1,2,3]] 回答1: You can think of reshaping that the new shape is filled row by row (last dimension varies fastest) from the flattened original list/array. An easy solution is to shape the list into a

Multiple column spread

天大地大妈咪最大 提交于 2019-12-13 10:06:15
问题 I have a need to do what is really what tidyr::spread() does, but for multiple value columns. If I have a data set like this: te <- structure(list(Syllable = c("[pa]", "[ta]", "[ka]", "[pa]", "[ta]", "[ka]", "[pa]", "[ta]", "[ka]", "[pa]"), PA = c(15.9252335141423, 2.17504491982172, 5.26727958979289, 4.48590068583509, 2.1316282072803e-13, 14.1415335887116, 3.51720477328246, 0.839953301362556, 5.74712643678048, 7.01396701583887), transient_mean = c(4.43699436235785, 4.8733556527069, 5

R: How to split a string into values and map the resultant broken pieces as columns to the dataset? [duplicate]

断了今生、忘了曾经 提交于 2019-12-13 09:18:05
问题 This question already has answers here : Split a column of concatenated comma-delimited data and recode output as factors (2 answers) Closed 2 years ago . As shown in the above pic, I've a column, genres, with a list of genres the corresponding movie belongs to. There are in total 19 unique genres. I'd like to know if I can manipulate this data into appending 19 columns to the data set each corresponding to each of the genres identifiers and label the corresponding cells as 0 or 1 indicating

Efficient way to reshape (alternately) thousands of data

▼魔方 西西 提交于 2019-12-13 07:24:51
问题 I have a data set which is very large, thousands of rows and hundreds of column. I try to alternately reshape the data for every nth row, and all the nth row column data. I tried like this: in=rand(71760,320); m=240; n=320; [R,C]=size(in); out=[]; R_out=R/m; for k=1:m %from row 1 to mth row for i=1:C %reshape every column of mth row out=[out;reshape(in(k:m:end,i),R_out,1)']; end end If you try out the code, it took very long time and not efficient at all, you won't even bother to let it

reshape data from wide to long with multiple rows

∥☆過路亽.° 提交于 2019-12-13 07:10:33
问题 I have a dataset dfs that i would like to reshape dfs # country.name indicator.name x1990 x1991 x1992 # 507 andorra GDP at market prices (current US$) 1.028989e+09 1.106891e+09 1.209993e+09 # 510 andorra GDP growth (annual %) 3.781393e+00 2.546001e+00 9.292154e-01 # 1347 albania GDP at market prices (current US$) 2.101625e+09 1.139167e+09 7.094526e+08 # 1350 albania GDP growth (annual %) -9.575640e+00 -2.958900e+01 -7.200000e+00 # 3587 austria GDP at market prices (current US$) 1.660624e+11 1

Casting multiple columns from one factor variable

£可爱£侵袭症+ 提交于 2019-12-13 04:59:07
问题 I have picked up an awful public data set that needs a lot of work to make it useful. Here is a simplification: Molten<-data.frame(ID=round(runif(100, 0, 50),0), Element=c(rep("Au", 20), rep("Fe", 10), rep("Al", 30),rep("Cu", 20),rep("Au", 20)), Measure=rnorm(100), Units=c(rep("ppm",10), rep("pct",10), rep("ppb", 80))) Molten$UnitElement<-paste(Molten$Element, Molten$Units, sep="_") Molten<-Molten[!duplicated(Molten[,c("ID", "Element")]),] I have arrived at a data frame with the IDs and a

Reshaping arrays in MATLAB

ε祈祈猫儿з 提交于 2019-12-13 04:37:37
问题 I have a binary 3D array of the size 1024 by 1024 by 1024. I want to use a function ( convhull ), which has the following input: X is of size mpts-by-ndim, where mpts is the number of points and ndim is the dimension of the space where the points reside, 2 ≦ ndim ≦ 3 How can I reshape my array into the array X which is required by this function? Maybe "reshape" isn't the best word, because using the "reshape" function isn't enough. 回答1: What convhull is looking for is a list of subscripts of

About reshaping numpy array

送分小仙女□ 提交于 2019-12-13 03:41:33
问题 trainX.size == 43120000 trainX = trainX.reshape([-1, 28, 28, 1]) (1)Does reshape accept a list as an argment instead of a tuple? (2)Are the following two statements equivalent? trainX = trainX.reshape([-1, 28, 28, 1]) trainX = trainX.reshape((55000, 28, 28, 1)) 回答1: Try the variations: In [1]: np.arange(12).reshape(3,4) Out[1]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) In [2]: np.arange(12).reshape([3,4]) Out[2]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) In [3]: np