这里介绍一种使用 difflib
中的 SequenceMatcher
的方法:
import pandas as pd
from difflib import SequenceMatcher
MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
def clean_date_time(date_time: str) -> str:
day, month, year = date_time.split(" ")
def get_similarity(other_month):
return SequenceMatcher(None, month, other_month).ratio()
return " ".join((day, max(MONTHS, key=get_similarity), year))
data["date"] = data["date"].apply(clean_date_time)
生成的数据
:
date
0 25 August 2014
1 14 August 2014
2 27 August 2014
3 18 March 2015
4 03 June 2014
w+
提取月份名称,尝试找到已知月份名称的最接近Levenshtein距离并替换它们...? - deceze