繁体   English   中英

将文本字段中的多个日期/时间值提取到新的变量列中

[英]Extract multiple date/time values from text field into new variable columns

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

我有 dataframe - 见下文。 这只是完整日期框架的一个片段,每个行/IDS 中都有更多文本和日期/时间。 如您所见,每个日期/时间前后的文本是随机的。

ID        RESULT
1         Patients Discharged Home :   12/07/2022 11:19 Bob Melciv   Appt 12/07/2022 12:19 Medicaid...
2         Stawword Geraldio -    12/17/2022 11:00 Bob Melciv   Appt 12/10/2022 12:09 Risk Factors...

我想从 RESULT 列中提取格式为MM/DD/YYYY HH:MM的所有日期/时间,并将每个相应的日期/时间放入它们自己的列中。

ID    DATE_TIME_1              DATE_TIME_2        DATE_TIME_3 .....
1     12/07/2022 11:19         12/07/2022 12:19
2     12/17/2022 11:00         12/10/2022 12:09

怎么样:

当然,这不包括无意义的日期,例如55/55/1023 ,但它应该让您完成 99% 的事情。

在 @David542 的正则表达式中,您可以使用str.extractall

pattern = r'(\d{2}/\d{2}/\d{4} \d{2}:\d{2})'
out = pd.concat([df['ID'],
                 df['RESULT'].str.extractall(pattern).squeeze()
                             .unstack().rename(columns=lambda x: f'DATE_TIME_{x+1}')
                             .rename_axis(columns=None)], axis=1)
print(out)

# Output
   ID       DATE_TIME_1       DATE_TIME_2
0   1  12/07/2022 11:19  12/07/2022 12:19
1   2  12/17/2022 11:00  12/10/2022 12:09

将提取的日期/时间转换为pd.DatetimeIndex的稍微修改的版本:

pattern = r'(\d{2}/\d{2}/\d{4} \d{2}:\d{2})'
out = pd.concat([df['ID'],
                 df['RESULT'].str.extractall(pattern).squeeze().apply(pd.to_datetime)
                             .unstack().rename(columns=lambda x: f'DATE_TIME_{x+1}')
                             .rename_axis(columns=None)], axis=1)
print(out)

# Output
   ID         DATE_TIME_1         DATE_TIME_2
0   1 2022-12-07 11:19:00 2022-12-07 12:19:00
1   2 2022-12-17 11:00:00 2022-12-10 12:09:00

一步步:

# 1. Date extraction (and squeeze DataFrame with 1 column to Series)
>>> out = df['RESULT'].str.extractall(pattern)
   match
0  0        12/07/2022 11:19
   1        12/07/2022 12:19
1  0        12/17/2022 11:00
   1        12/10/2022 12:09
Name: 0, dtype: object

# 2. Move second index level as column (and add the prefix DATE_TIME_N)
>>> out = out.unstack().rename(columns=lambda x: f'DATE_TIME_{x+1}')
match       DATE_TIME_1       DATE_TIME_2
0      12/07/2022 11:19  12/07/2022 12:19
1      12/17/2022 11:00  12/10/2022 12:09

# 3. Remove the 'match' title on column axis
>>> out = out.rename_axis(columns=None)
            DATE_TIME_1       DATE_TIME_2
0      12/07/2022 11:19  12/07/2022 12:19
1      12/17/2022 11:00  12/10/2022 12:09

最后沿列轴将原始 ID 与这个新的 dataframe 连接起来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM