【发布时间】:2020-11-09 03:07:13
【问题描述】:
我正在尝试从个人健康记录中解析日期。由于条目似乎是手动的,因此日期格式无处不在。我的正则表达式模式显然没有在几个观察中得到削减。这是我需要完成的任务列表以及随附的代码。为方便起见,Dataframe 已被细分为 15 个观察值。
- 解析日期:
#Create DF:
health_records = ['08/11/78 CPT Code: 90801 - Psychiatric Diagnosis Interview',
'Lithium 0.25 (7/11/77). LFTS wnl. Urine tox neg. Serum tox + fluoxetine 500; otherwise neg. TSH 3.28. BUN/Cr: 16/0.83. Lipids unremarkable. B12 363, Folate >20. CBC: 4.9/36/308 Pertinent Medical Review of Systems Constitutional:',
'28 Sep 2015 Primary Care Doctor:',
'06 Mar 1974 Primary Care Doctor:',
'none; but currently has appt with new HJH PCP Rachel Salas, MD on October. 11, 2013 Other Agency Involvement: No',
'.Came back to US on Jan 24 1986, saw Dr. Quackenbush at Beaufort Memorial Hospital. Checked VPA level and found it to be therapeutic and confirmed BPAD dx. Also, has a general physician exam and found to be in good general health, except for being slightly overwt',
'September. 15, 2011 Total time of visit (in minutes):',
'sLanguage based learning disorder, dyslexia. Placed on IEP in 1st grade through Westerbrook HS prior to transitioning to VFA in 8th grade. Graduated from VF Academy in May 2004. Attended 1.5 years college at Arcadia.Employment Currently employed: Yes',
') - Zoloft 100 mg daily: February, 2010 : self-discontinued due to side effects (unknown)',
'6/1998 Primary Care Doctor:',
'12/2008 Primary Care Doctor:',
'ran own business for 35 years, sold in 1985',
'011/14/83 Audit C Score Current:',
'safter evicted in February 1976, hospitalized at Pemberly for 1 mo.Hx of Outpatient Treatment: No',
'. Age 16, 1991, frontal impact. out for two weeks from sports.',
's Mr. Moss is a 27-year-old, Caucasian, engaged veteran of the Navy. He was previously scheduled for an intake at the Southton Sanitorium in January, 2013 but cancelled due to ongoing therapy (see Psychiatric History for more details). He presents to the current intake with primary complaints of sleep difficulties, depressive symptoms, and PTSD.']
import numpy as np
import pandas as pd
df = pd.DataFrame(health_records, columns=['records'])
#Date parsing: patten 1:
df['new'] = (df['records'].str.findall(r'\d{1,2}.\d{1,2}.\d{2,4}')
.astype(str).str.replace(r'\[|\]|\(|\)|,|\'', '').str.strip())
#Date parsing pattern 2:
df['new2'] = (df['records'].str.findall(r'(?:\d{2} )?(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]* (?:\d{2}, )?\d{4}')
.astype(str).str.replace(r'\[|\]|\'|\,','').str.strip())
df['date'] = df['new']+df['new2']
这是输出:
df['date']
0 08/11/78
1 7/11/77 16/0.83 4.9/36
2 28 Sep 2015
3 06 Mar 1974
4
5 24 1986
6
7 May 2004
8
9 6/1998
10 12/2008
11
12 011/14
13 February 1976
14
15
您可以在某些地方看到代码完美运行,但在复杂的句子中,我的模式不起作用或吐出不准确的结果。以下是所有可能的日期组合的列表:
- 2009 年 4 月 20 日; 09 年 4 月 20 日; 2009 年 4 月 20 日; 2009 年 4 月 3 日;
- 2009 年 3 月 20 日; 2009 年 3 月 20 日; 2009 年 3 月 20 日; 2009年3月20日; 2009 年 3 月 20 日;
- 2009 年 3 月 20 日; 2009 年 3 月 20 日; 2009 年 3 月 20 日; 2009 年 3 月 20 日 2009 年 3 月 20 日;
- 2009 年 3 月 21 日; 2009 年 3 月 22 日;
- 2009 年 2 月; 2009 年 9 月; 2010 年 10 月; 6/2008; 2009 年 12 月 12 日
- 2009; 2010
- 清空日期
接下来我尝试使用here 提供的解决方案来清理日期 - 虽然它应该可以工作,因为我的格式与该问题中的格式相似,但它没有。
#Clean dates to date format
df['clean_date'] = df.date.apply(
lambda x: pd.to_datetime(x).strftime('%m/%d/%Y'))
df['clean_date']
上面的代码不起作用。任何帮助将不胜感激。感谢您的宝贵时间!
【问题讨论】: