Transpose the data in a column every nth rows in PANDAS

If no data are missing, you can use numpy.reshape: print (np.reshape(df.values,(2,5))) [[‘Andrew’ ‘School of Music’ ‘Music: Sound of the wind’ ‘Dr. Seuss’ ‘Dr.Sass’] [‘Michelle’ ‘School of Theatrics’ ‘Music: Voice’ ‘Dr. A’ ‘Dr. B’]] print (pd.DataFrame(np.reshape(df.values,(2,5)), columns=[‘Name’,’School’,’Music’,’Mentor1′,’Mentor2′])) Name School Music Mentor1 Mentor2 0 Andrew School of Music Music: Sound of the wind Dr. Seuss Dr.Sass 1 … Read more

load csv into 2D matrix with numpy for plotting

Pure numpy numpy.loadtxt(open(“test.csv”, “rb”), delimiter=”,”, skiprows=1) Check out the loadtxt documentation. You can also use python’s csv module: import csv import numpy reader = csv.reader(open(“test.csv”, “rb”), delimiter=”,”) x = list(reader) result = numpy.array(x).astype(“float”) You will have to convert it to your favorite numeric type. I guess you can write the whole thing in one line: … Read more

What methods can we use to reshape VERY large data sets?

If your real data is as regular as your sample data we can be quite efficient by noticing that reshaping a matrix is really just changing its dim attribute. 1st on very small data library(data.table) library(microbenchmark) library(tidyr) matrix_spread <- function(df1, key, value){ unique_ids <- unique(df1[[key]]) mat <- matrix( df1[[value]], ncol= length(unique_ids),byrow = TRUE) df2 <- … Read more

Is there an R function to reshape this data from long to wide?

You could create a new identifier column with unique value for every student and then use pivot_wider to case multiple columns to wide. library(dplyr) df %>% mutate(name = as.integer(factor(Student))) %>% tidyr::pivot_wider(names_from = name, values_from = c(Student, score)) # CoachID Student_1 Student_2 Student_3 score_1 score_2 score_3 # <int> <fct> <fct> <fct> <int> <int> <int> #1 1 … Read more