我的数据集如下所示:
Name : joe
Job : Crazy Consultant
Hired : 4/12/2011 3:38:55 AM
Stats : crazy, bald head
Pay : $5000 Monthly
Name : Matt
Job : Crazy Receptionist
Hired : 4/12/2014 3:38:55 PM
Stats : crazy, Lots of hair
Name : Adam
Job : Crazy Drinker
Hired : 4/12/2017 3:38:55 AM
Stats : crazy, unknown
Term : 4/12/2017 3:38:55 PM
我读入并获取如下数据:
df = pd.read_csv(r"pathtomycsv.csv", encoding="UTF-16", delimiter='\s+:').transpose()
上述输出:(仅作为示例)
Name Job Hired Stats Name Job Hired Stats
Joe Crazy Consultant 4/12/2011 3:38:55 AM crazy, bald head Matt Crazy Consultant 4/12/2011 3:38:55 AM crazy, bald head
最终,我想从上面获取我的数据集,并通过将所有标题组合在一起将其转换为如下所示的数据集,如下所示:
Name Job Hired Stats Pay Term
Joe Crazy Consultant 4/12/2011 3:38:55 AM crazy, bald head $5000 Monthly N/A
Matt Crazy Receptionist 4/12/2014 3:38:55 PM crazy, Lots of hair N/A N/A
Adam Crazy Drinker 4/12/2017 3:38:55 AM crazy, unknown N/A 4/12/2017 3:38:55 PM
答案 0 :(得分:3)
出现问题是因为您在日期中有更多冒号。使用"\s+:\s+"
作为分隔符。 (是的,它可以是正则表达式。)
以下代码可用于将您的文件转换为所需的表格。我认为'姓名'始终是集合中的第一行。
df = pd.read_csv("yourfile", delimiter='\s+:\s+',header=None)
df = df.reset_index()
df['index'][df[0]!='Name'] = np.nan
df['index'] = df['index'].fillna(method='ffill').astype(int)
df.set_index(['index',0])[1].unstack().set_index('Name')
#0 Hired Job Pay
#Name
#joe 4/12/2011 3:38:55 AM Crazy Consultant $5000 Monthly
#Matt 4/12/2014 3:38:55 PM Crazy Receptionist None
#Adam 4/12/2017 3:38:55 AM Crazy Drinker None
答案 1 :(得分:1)