如下所示,有大量空格,从行首、行尾到行中都有。我正在尝试从中间删除这些额外的空格。以下是我的尝试,但我一直收到错误提示:
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()
我该怎么移除这个?谢谢!
Clean Output:
'Sea Ice Prediction Network .'
'Movies, Ratings, .... etc.'
'Iceland, Greenland, Mountains '
'My test file'
testdata
中? - ayhan