Python中出现’float’ object has no attribute 'strip’的解决方式

我的原代码是这样的

document = pd.read_csv("sample_data.csv",encoding="utf-8")
#document.columns  = ['sentences'] # 重命名列名
document['公众号文章内容'] = document['公众号文章内容'].apply(lambda x: x.strip()) # 去除文章内容前后的空白
document['文章标题'] = document['文章标题'].apply(lambda x: x.strip()) # 去除文章标题前后的空白
document['lens'] = document['公众号文章内容'].apply(len) # 统计文本长度
document = document[document['lens']>1] # 去除文本长度小于等于1的文本
document.index = np.arange(len(document))

运行后出现了这样的错误:
在这里插入图片描述
解决方法:
可以在打开文件的后面加上.astype(str)就可

document = pd.read_csv("sample_data.csv",encoding="utf-8").astype(str)

在运行错误就没有了

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐