2017-10-30 8 views
1

아래 팬더 데이터 프레임이 있는데 [File Name ","File Start Time "등] 열 머리글로 정렬하고 싶습니다. 문자열을 찾는 행을 반복하는 루프가 있다고 상상할 수도 있지만, 여기에는 더 간단한 옵션이있을 것입니다.열에 반복 된 헤더가 포함 된 경우 팬더 테이블 다시 포맷

import pandas as pd 

data = pd.read_csv(file_path + 'chb01-summary.txt',skiprows = 28, header=None, delimiter = ": ") 

파일 소스 당신은 read_csv를 사용하고 unstack에 의해 바꿀 수 https://www.physionet.org/pn6/chbmit/chb01/chb01-summary.txt

enter image description here

답변

1

:

url = 'https://www.physionet.org/pn6/chbmit/chb01/chb01-summary.txt' 
df = pd.read_csv(url, skiprows=28, sep=':\s+', names=['a','b'], engine='python') 
print (df.head()) 
          a    b 
0     File Name chb01_01.edf 
1    File Start Time  11:42:54 
2    File End Time  12:42:54 
3 Number of Seizures in File    0 
4     File Name chb01_02.edf 

df = df.set_index([df['a'].eq('File Name').cumsum(), 'a'])['b'] 
     .unstack() 
     .reset_index(drop=True) 
print (df.head()) 
a File End Time  File Name File Start Time Number of Seizures in File \ 
0  12:42:54 chb01_01.edf  11:42:54       0 
1  13:42:57 chb01_02.edf  12:42:57       0 
2  14:43:04 chb01_03.edf  13:43:04       1 
3  15:43:12 chb01_04.edf  14:43:12       1 
4  16:43:19 chb01_05.edf  15:43:19       0 

a Seizure End Time Seizure Start Time 
0    None    None 
1    None    None 
2  3036 seconds  2996 seconds 
3  1494 seconds  1467 seconds 
4    None    None 
+0

어떤 문제가? 설명 할 수 있니? – jezrael