I have a column in a dataframe with text. The text has many dates, I want to add a newline before and after the date. I have tried with regex, I have found all the dates and replaced them with '\n'+date+'\n'. The problem is that now, in Excel, when I double-click on the cell containing (to expend it) and then click outside of it - Excel automatically wraps text in that cell.
So i thought that maybe the '\n' would be the issue and that maybe there is another character that Excel would interpret as ALT+Enter. (I have seen lots of similar questions but they mostly give either the '\n' or wrap text answer).
df example:
{'type’': {4: 'a', 5: 'a', 6: 'a', 7:'a'},
'development': {4: "28/03. some text goes here. 08/04. some text goes here 06/04. some text goes here - 02/04. some text goes here.",
5: nan,
6: nan
7: "23/06. some text goes here. 08/04. some text goes here 06/04. some text goes here - 02/04. some text goes here."}}
What I have done is this (which I'm sure is quite a slow way of doing it, maybe it would be better with re.sub or with pandas string methods, but the df is small enough and this does the job!)
new_val = []
val = df['development'].tolist()
for s in val:
ms = re.findall('\d+/\d+', str(s))
for m in ms:
s = s.replace(m, '\n'+m+'\n')
new_val.append(s)
df['development'] = new_val