reshape

How to create relational matrix in R?

被刻印的时光 ゝ 提交于 2019-12-23 20:12:52
问题 Original data: df <- structure(list(ID_client = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("1_", "2_", "3_", "4_"), class = "factor"), Connected = c(1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L), Year = c(2010L, 2010L, 2010L, 2010L, 2015L, 2015L, 2015L, 2015L)), class = "data.frame", row.names = c(NA, -8L)) Original data: `ID_client Connected Year 1_ 1 2010 2_ 1 2010 3_ 1 2010 4_ 0 2010 1_ 1 2015 2_ 0 2015 3_ 1 2015 4_ 0 2015` My intention is to create the following data: `Year ID_client 1_ 2_

How do I use the reshape() function more than once successfully in R? [duplicate]

风流意气都作罢 提交于 2019-12-23 19:28:08
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 2 years ago . This is my dataframe: ID Group x1 x2 x3 y1 y2 y3 z1 z2 z3 144 1 566 613 597 563 549 562 599 82 469 167 2 697 638 756 682 695 693 718 82 439.5 247 4 643 698 730 669 656 669 698 82 514.5 317 4 633 646 641 520 543 586 559 82 405.5 344 3 651 678 708 589 608 615 667 82 514 352 2 578 702 671 536 594 579 591 82 467.5 382 1 678 690

Create new columns in pandas from python nested lists

安稳与你 提交于 2019-12-23 15:44:11
问题 I have a pandas data frame. One of the columns has a nested list. I would like to create new columns from the nested list Example: L = [[1,2,4], [5,6,7,8], [9,3,5]] I want all the elements in the nested lists as columns. The value should be one if the list has the element and zero if it does not. 1 2 4 5 6 7 8 9 3 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 0 0 0 1 1 回答1: You can try the following: df = pd.DataFrame({"A": L}) df # A #0 [1, 2, 4] #1 [5, 6, 7, 8] #2 [9, 3, 5] # for each cell,

Python Pandas Wide to Long Format Change with Column Titles Spliting

别等时光非礼了梦想. 提交于 2019-12-23 09:56:58
问题 I have a table with the following columns titles and a row example: Subject Test1-Result1 Test1-Result2 Test2-Result1 Test2-Result2 0 John 10 0.5 20 0.3 I would like to transform it to: Subject level_1 Result1 Result2 0 John Test1 10 0.5 1 John Test2 20 0.3 With the subjects list repeated once for Test1 and then again for Test2. I think I can do this using for loops, but it's there a more pythonic way? For extra complexity, I need to add an extra column of information for each test. I suppose

Reshape vector with a step and window size

十年热恋 提交于 2019-12-23 04:59:37
问题 I have a vector, for example A = [1 2 3 4 5 6 7 8] I want to "reshape" it to matrix with windowsize=4 and stepsize=2 , such that the resulting matrix is b = [ 1 3 5; 2 4 6; 3 5 7; 4 6 8 ] 回答1: You can set up an indexing matrix, then just index into A ... A = [1 2 3 4 5 6 7 8]; windowsize = 4; stepsize = 2; % Implicit expansion to create a matrix of indices idx = bsxfun( @plus, (1:windowsize).', 0:stepsize:(numel(A)-windowsize) ); b = A(idx); Note; in this case idx and b are the same, but you

pandas long to wide multicolumn reshaping

我只是一个虾纸丫 提交于 2019-12-23 01:54:52
问题 I have a pandas data frame as follows: request_id crash_id counter num_acc_x num_acc_y num_acc_z 745109.0 670140638.0 0 0.010 0.000 -0.045 745109.0 670140638.0 1 0.016 -0.006 -0.034 745109.0 670140638.0 2 0.016 -0.006 -0.034 my id vars are : "request_id" and "crash_id", the target vars are nu_acc_x, num_acc_y and num_acc_z I would like to create a new DataFrame where target vars are wide reshaped, that is adding max(counter)*3 new vars like num_acc_x_0, num_acc_x_1, ... num_acc_y_0,num_acc_y

How to reshape data to long format?

我们两清 提交于 2019-12-23 01:47:07
问题 I have a .csv file like this: +-------+---------+------+-------+ | CONN | TABLE | COLS | OWNER | +-------+---------+------+-------+ | ONE | TABLE_A | 10 | MIKE | | ONE | TABLE_B | 9 | MIKE | | ONE | TAB_A | 11 | KIM | | ONE | TAB_B | 14 | KIM | | TWO | TABLE_A | 9 | MIKE | | TWO | TABLE_B | 9 | MIKE | | TWO | TAB_A | 11 | KIM | | TWO | TAB_D | 56 | KIM | | THREE | TABLE_A | 9 | MIKE | | THREE | TABLE_C | 3 | MIKE | | THREE | TABLE_D | 11 | KIM | | THREE | TAB_A | 11 | KIM | +-------+---------

Convert dataframe from wide to long - pandas

被刻印的时光 ゝ 提交于 2019-12-23 01:44:05
问题 I have a dataframe like this: index S_1 S_2 S_3 S_4 0 1 0 0 1 1 1 1 Nan Nan I am trying to change it from long to wide. Eg. index num S 0 1 1 0 2 0 0 3 0 0 4 1 1 1 1 1 2 1 1 3 Nan 1 4 Nan I have tried the following code, based on this answer, but I get the following error: matches_df.columns = matches_df.columns.str.split('_', expand=True) TypeError: object of type 'float' has no len() Why am I unable to split on the "_"? There is other information in the columns which I would like to

Create New Data Frame with Column Names from Unique Values in another Data Frame and Corresponding Values Assigned to Column

穿精又带淫゛_ 提交于 2019-12-22 10:42:25
问题 I'm new to R, and I'm pretty sure this is something simple to accomplish, but I cannot figure out how to perform this action. I've tried the split function, utilizing a for loop, but cannot quite figure out how to get it right. As an example, this is what my original data frame looks like: dat <- data.frame(col1 = c(rep("red", 4), rep("blue", 3)), col2 = c(1, 3, 2, 4, 7, 8, 9)) col1 col2 red 1 red 3 red 2 red 4 blue 7 blue 8 blue 9 I want to create new columns for each unique value in col1

Is there a way to reshape an array that does not maintain the original size (or a convenient work-around)?

一笑奈何 提交于 2019-12-22 09:39:56
问题 As a simplified example, suppose I have a dataset composed of 40 sorted values. The values of this example are all integers, though this is not necessarily the case for the actual dataset. import numpy as np data = np.linspace(1,40,40) I am trying to find the maximum value inside the dataset for certain window sizes. The formula to compute the window sizes yields a pattern that is best executed with arrays (in my opinion). For simplicity sake, let's say the indices denoting the window sizes