Transpose data in a column every n-th row in PANDAS

For a research project, I need to process every single information from a website in an excel file. I copied and embedded everything I need from the site into one column in the excel file, and I downloaded this file using PANDAS. However, I need to present each individual information horizontally, and not vertically, as it is now. For example, this is what I have right now. I have only one column of unorganized data.

df= pd.read_csv("ior work.csv", encoding = "ISO-8859-1")

Data:

0 Andrew
1 School of Music
2 Music: Sound of the wind
3 Dr. Seuss
4 Dr.Sass
5 Michelle
6 School of Theatrics
7 Music: Voice
8 Dr. A
9 Dr. B

I want to transpose every 5 lines in order to organize the data in this organizational format; column labels are indicated below.

Name School Music Mentor1 Mentor2

What is the most efficient way to do this?

+4
1

, 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  Michelle  School of Theatrics              Music: Voice      Dr. A    Dr. B

length array shape :

print (pd.DataFrame(np.reshape(df.values,(df.shape[0] / 5,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  Michelle  School of Theatrics              Music: Voice      Dr. A    Dr. B

piRSquared :

print (pd.DataFrame(df.values.reshape(-1, 5), 
                    columns=['Name','School','Music','Mentor1','Mentor2']))
+2

Source: https://habr.com/ru/post/1656164/


All Articles