I have a pandas dataframe with about 20 columns.
It is possible to replace all occurrences of a string (here a newline) by manually writing all column names:
df['columnname1'] = df['columnname1'].str.replace("\n","<br>")
df['columnname2'] = df['columnname2'].str.replace("\n","<br>")
df['columnname3'] = df['columnname3'].str.replace("\n","<br>")
...
df['columnname20'] = df['columnname20'].str.replace("\n","<br>")
This unfortunately does not work:
df = df.replace("\n","<br>")
Is there any other, more elegant solution?
This will remove all newlines and unecessary spaces. You can edit the ' '.join to specify a replacement character
You can use
replace
and pass the strings to find/replace as dictionary keys/items:For example:
It seems Pandas has change its API to avoid ambiguity when handling regex. Now you should use:
For example: