As seen below, there are ton of whitespaces, starting, ending, middle of the lines. I am trying to remove these extra whitespaces from the middle. Here is what I tried, but I keep getting error like:
testdata = [{'col1': ' Sea Ice Prediction Network . '},
{'col1': ' Movies, Ratings, .... etc.'},
{'col1': 'Iceland, Greenland, Mountains '},
{'col1': ' My test file'}]
df = pd.DataFrame(testdata)
' '.join(testdata['col1'].split()) #Error: list indices must be integers or slices, not str
df['col1'].str.lstrip() #list indices must be integers or slices, not str
df['col1'].str.rstrip() #list indices must be integers or slices, not str
#removes start and end, but not ideal to remove one line at a time.
' Sea Ice Prediction Network . '.lstrip()
' Sea Ice Prediction Network . '.rstrip()
How do I remove this? Thanks!
Clean Output:
'Sea Ice Prediction Network .'
'Movies, Ratings, .... etc.'
'Iceland, Greenland, Mountains '
'My test file'