Text cleaning: removing dates from text, where there are

Viewed 83

I would need to clean some text which may contain a sequence like this:

Date            Text
2020-06-01  1 giu 2020 - Per farlo hai messo in atto un pia...
2020-06-01  La trappola del Paragone
2020-06-05  5 giu 2020 - sistema Aegis Ashore? N...
2020-06-05  Lewis Hamilton - «Il silenzio colpevole della F1»
  

There might be texts which starts with a date (format 1 giu 2020, i.e. d m yyyy, followed by -) and texts which do not. I would like to know how to keep only the part of text, i.e.

Date            Text
2020-06-01  Per farlo hai messo in atto un pia...
2020-06-01  La trappola del Paragone
2020-06-05  sistema Aegis Ashore? N...
2020-06-05  Lewis Hamilton - «Il silenzio colpevole della F1»

I have tried as follows:

fil_dataset['Text']=fil_dataset['Text'].str.split(n=4).str[-1] but does not work as it can remove also part of text. A possible solution, proposed by anky in his/her comment below, unfortunately does not work as well, as I need also to keep in mind that there might be a -, which does not follow a date, that could remove some text.

Thank you for your help.

2 Answers

You can use str.replace:

print (df["Text"].str.replace("\d{1,2}\s\w*\s\d{4}\s-\s",""))

0                Per farlo hai messo in atto un pia...
1                             La trappola del Paragone
2                           sistema Aegis Ashore? N...
3    Lewis Hamilton - «Il silenzio colpevole della F1»

Use a regexp like r'\d{1,2} (giu|mai|abc|xyz) 20\d\d ', with other Italian(?) month abbreviations inserted.

I don't recall off hand how to apply this as a filter in Pandas but hopefully you can work that out.

Related