当列包含重复的标题时重新格式化pandas表

时间:2017-10-30 12:47:40

标签: pandas dataframe reformatting

我有下面的pandas DataFrame,我想对它进行排序,使得[“文件名”,“文件开始时间”等]是列标题。我可以想象在遍历查找字符串的行中运行循环,但也许有一个更简单的选项吗?

import pandas as pd

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

档案来源https://www.physionet.org/pn6/chbmit/chb01/chb01-summary.txt

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用read_csv并按unstack重新整形:

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