reshape

Recasting nested list of any depth

杀马特。学长 韩版系。学妹 提交于 2021-02-19 06:06:54
问题 Assume this simplified example: L <- list() L$Foo <- list() L$Foo$Bar <- list() L$Foo$Bar$VAR <- TRUE L$Lorem <- list() L$Lorem$Ipsum <- list() L$Lorem$Ipsum$Dolor <- list() L$Lorem$Ipsum$Dolor$VAR <- TRUE I will then melt this list with reshape2::melt(L) . That will output the following: value L3 L2 L4 L1 1 TRUE VAR Bar <NA> Foo 2 TRUE Dolor Ipsum VAR Lorem After some operations on certain cells in the value column, I'm then looking to recast this melted list into the exact same nested list

R Help converting factor data from long to wide and assigning logical value

别来无恙 提交于 2021-02-17 05:54:28
问题 I have data in long format as seen below: Data: id code 1 EP 2 EP 3 EP 4 UM 5 UM 1 UM 2 UM 10 UM 6 BZ 7 BZ 14 BZ 2 BZ 8 TVOL 9 TVOL 16 TVOL 10 NW 11 NW 7 NW 12 SM 13 SM 3 SM 14 GS 15 GS 1 GS 2 GS 9 GS I would like to create a wide dataframe with each "code" as its own column marked TRUE/FALSE depending on whether there's an associated "id" as seen in the minimal example below: id code.EP code.UM code.BZ code.TVOL code.NW code.SM code.GS 1 TRUE TRUE FALSE FALSE FALSE FALSE TRUE 2 TRUE FALSE

Numpy reshape an array with specific order

人盡茶涼 提交于 2021-02-17 04:26:25
问题 Let's say I have this array x: x = array([1, 2, 3, 4, 5, 6, 7, 8]) x.shape = (8,1) I want to reshape it to become array([[1, 3, 5, 7], [2, 4, 6, 8]]) this is a reshape(2, 4) on x but in the straight forward way: y = x.reshape(2,4) y becomes array([[1, 2, 3, 4], [5, 6, 7, 8]]) and that's not what I want. Is there a way to transform the array in that specific way? 回答1: In[4]: x.reshape(4, 2).T Out[4]: array([[1, 3, 5, 7], [2, 4, 6, 8]]) 回答2: The easiest way to do this is to specify the order

ValueError: cannot reshape array of size 7267 into shape (302,24,1)

风格不统一 提交于 2021-02-17 02:07:30
问题 I am reshaping a 1D array into 3D using the following. It works fine but it throws an error when x is 7267. I understand that it is not possible to slice an odd number as an int without losing some values. Would appreciate any solution to this. code x = 7248 y= 24 A = np.arange(x) A.reshape(int(x/y),y,1).transpose() output array([[[ 0, 24, 48, ..., 7176, 7200, 7224], [ 1, 25, 49, ..., 7177, 7201, 7225], [ 2, 26, 50, ..., 7178, 7202, 7226], ..., [ 21, 45, 69, ..., 7197, 7221, 7245], [ 22, 46,

Transpose the data in a column every nth rows in PANDAS

心不动则不痛 提交于 2021-02-16 07:52:31
问题 For a research project, I need to process every individual's information from the website into an excel file. I have copied and pasted everything I need from the website onto a single column in an excel file, and I loaded that file using PANDAS. However, I need to present each individual's information horizontally instead of vertically like it is now. For example, this is what I have right now. I only have one column of unorganized data. df= pd.read_csv("ior work.csv", encoding = "ISO-8859-1"

Transpose the data in a column every nth rows in PANDAS

半城伤御伤魂 提交于 2021-02-16 07:49:21
问题 For a research project, I need to process every individual's information from the website into an excel file. I have copied and pasted everything I need from the website onto a single column in an excel file, and I loaded that file using PANDAS. However, I need to present each individual's information horizontally instead of vertically like it is now. For example, this is what I have right now. I only have one column of unorganized data. df= pd.read_csv("ior work.csv", encoding = "ISO-8859-1"

Reshaping a table in R while parsing information from column names and using it to collect information from specific columns

冷暖自知 提交于 2021-02-11 13:00:22
问题 I have this badly organized data table given to me, in which there are hundreds of columns (subset is given below) Names of columns are dot delimited where the first field holds information about a type of object (e.g. Item123, object_AB etc.) without any naming convention. There is no specific order for these columns as well. Other columns share the type of object field and also have the name of some property for that object (e.g. color, manufacturer etc.). Item123.type.value Item123.mass

Numpy array - stack multiple columns into one using reshape

走远了吗. 提交于 2021-02-10 18:54:30
问题 For a 2D array like this: table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]]) Is it possible to use np.reshape on table to get an array single_column where each column of table is stacked vertically? This can be accomplished by splitting table and combining with vstack . single_column = np.vstack(np.hsplit(table , table .shape[1])) Reshape can combine all the rows into a single row, I'm wondering if it can combine the columns as well to make the code cleaner and possibly faster.

Numpy array - stack multiple columns into one using reshape

旧时模样 提交于 2021-02-10 18:53:35
问题 For a 2D array like this: table = np.array([[11,12,13],[21,22,23],[31,32,33],[41,42,43]]) Is it possible to use np.reshape on table to get an array single_column where each column of table is stacked vertically? This can be accomplished by splitting table and combining with vstack . single_column = np.vstack(np.hsplit(table , table .shape[1])) Reshape can combine all the rows into a single row, I'm wondering if it can combine the columns as well to make the code cleaner and possibly faster.

reshape a pandas dataframe with multiple columns

坚强是说给别人听的谎言 提交于 2021-02-10 18:21:45
问题 I have an issue in reshaping a pandas DatFrame. It looks like this (the numbers of lines and columns can vary) : columns col1 col2 col3 col4 Species sp1 218.000000 521.000000 533.000000 793.000000 sp1 0.105569 0.252300 0.258111 0.384019 sp1 2 2 2 3 sp2 225.000000 521.000000 540.000000 800.000000 sp2 0.107862 0.249760 0.258869 0.383509 sp2 2 2 2 3 sp3 217.000000 477.000000 512.000000 725.000000 sp3 0.112377 0.247022 0.265148 0.375453 sp3 1 1 3 3 The column Species is my index. I want to