在 PANDAS 中每第 n 行转置一列中的数据 [英] Transpose the data in a column every nth rows in PANDAS
问题描述
对于一个研究项目,我需要将网站上每个人的信息处理成一个 excel 文件.我已将网站上所需的所有内容复制并粘贴到 excel 文件中的单个列中,然后使用 PANDAS 加载该文件.但是,我需要水平呈现每个人的信息,而不是像现在这样垂直呈现.例如,这就是我现在所拥有的.我只有一列无组织的数据.
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")
数据:
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
我想每5行转置一次,将数据组织成这种组织格式;下面的标签是列的标签.
I want transpose every 5 lines to organize the data into this organizational format; the labels below are labels of the columns.
Name School Music Mentor1 Mentor2
最有效的方法是什么?
推荐答案
如果没有数据缺失,可以使用 numpy.reshape
:
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 Michelle School of Theatrics Music: Voice Dr. A Dr. B
通过shape
除以列数:
More general solution with generating length
of new array
by shape
divide by number of columns:
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 另一个解决方案:
Thank you piRSquared for another solution:
print (pd.DataFrame(df.values.reshape(-1, 5),
columns=['Name','School','Music','Mentor1','Mentor2']))
这篇关于在 PANDAS 中每第 n 行转置一列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!