code:
df['Rep'] = df['Rep'].str.replace('\\n', ' ')
issue:
if the df[‘Rep’] is empty or null ,there will be an error:
Failed: Can only use .str accessor with string values!
is there anyway can handle the situation when the column value is empty or null? If it is empty or null ,just ignore that row
>Solution :
By default the empty series dtype will be float64.
You can do a workaround using the astype:
df['Rep'] = df['Rep'].astype('str').str.replace('\\n', ' ')
Test code:
df = pd.DataFrame({'Rep': []})
# works
df['Rep'] = df['Rep'].astype('str').str.replace('\\n', ' ')
# doesn't work
df['Rep'] = df['Rep'].str.replace('\\n', ' ')
I don’t know which version of pandas you are using by they will changed to object the default dtype.
Edit: Still won’t work with object, just tested using the latest version of pandas (2.0.0).