首页 > 解决方案 > 无法将多个日期时间对象提取到数据框中的一个单元格中

问题描述

我有几句话,我将每个句子放在数据框的一行中。我正在研究从这些句子中提取日期。我遇到了“datefinder”这个包。

当我将单个句子发送到“string_with_dates”时,它会正确提取所有日期并返回。

import datefinder
string_with_dates = '''  They have released Proposals for period October 1, 2018 ’ September 30, 2019. Manufacturers are encouraged to submit proposals for stores located basis throughout the fiscal year ending September 30, 2018, pending availability of funds., '''

matches = datefinder.find_dates(string_with_dates)
for match in matches:
    match = str(match)
    print(match)

output = 2018-10-01 00:00:00
         2019-09-30 00:00:00
         2018-09-30 00:00:00

但是,当我放置一个数据框的多个句子并使用“for”循环进行循环时,它就会变得一团糟。它不会在数据框的单元格中正确显示多个日期(如果有)。description_df 是我的数据框的名称。在第 9 列中,我有句子,在第 13 列中,我希望存储提取的日期。

    import datefinder
    for i in range (len(description_df)):
        string_with_dates = description_df.iloc[i,9]
        matches = datefinder.find_dates(string_with_dates)
        for match in matches:
            match = str(match)
            print(match)
            description_df.iloc[i,13] =  match
Output of the extracted date column of the dataframe is:
2019-09-30 00:00:00
2019-05-07 00:00:00
""
0310-08-07 00:00:00
2019-08-07 00:00:00

标签: python-3.xdatefinder

解决方案


推荐阅读