PANDAS

2016-09-29 6 views
4

의 n 번째 행마다 데이터를 조 변경 연구 프로젝트의 경우 웹 사이트의 모든 개인 정보를 Excel 파일로 처리해야합니다. 필자는 웹 사이트에서 필요한 모든 것을 Excel 파일의 단일 열로 복사하여 붙여 넣었으며 PANDAS를 사용하여 해당 파일을로드했습니다. 그러나 나는 지금처럼 각 개인의 정보를 수직으로가 아닌 수평으로 제시해야한다. 예를 들어, 이것이 지금 제가 가지고있는 것입니다. 나는 하나의 컬럼에 조직화되지 않은 데이터만을 가지고있다.PANDAS

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

데이터 :이 조직 형식으로 데이터를 구성하기 위해 매 5 선을 바꾸어 원하는

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 

; 아래의 레이블은 열의 레이블입니다.

Name School Music Mentor1 Mentor2 

가장 효과적인 방법은 무엇입니까? 데이터가 누락되지 않으면

답변

2

, 당신은 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 

보다 일반적인 솔루션을 새로운 arraylength를 생성하여 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'])) 
+1

'pdf.DataFrame (df.values.reshape (-1, 5), columns = [ '이름', '학교', '음악', '멘토 1', '멘토 2]'))' – piRSquared

+0

고맙습니다. jezrael 및 @piRSquared! –