Try (without the for loop):
df1 = df1.apply(lambda x: x.str.strip())
df2 = df2.apply(lambda x: x.str.strip())
Or a bit less verbose:
strip = lambda s: s.str.strip()
df1.apply(strip)
df2.apply(strip)
Or with replace
:
trailings = ['^s+', 's+$']
df1.replace(trailings, '', regex=True)
df2.replace(trailings, '', regex=True)
If you want to use loop, then update the data of the dataframe, instead of reassigning them:
list_df = [df1, df2]
for df in [df1,df2]:
# df = df.apply(strip) wouldn't work
df[:] = df.apply(strip)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…