只能对字符串值使用.str访问器

2024-06-16 09:51:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用一个函数剥离多个数据帧中的所有字符串列 这是密码

def stripped_data(data):
    data=data.apply(lambda x: x.str.strip() if x.dtype=="object" else x)
    return data

然后我尝试使用locals()字典在同一个函数中传递多个表

for i in files_new:
    locals()[i]=stripped_data(locals()[i])

这里是包含表名的列表 但这就是错误——“只能使用带字符串值的.str访问器!”

但是这段代码单独运行时没有任何错误

data=data.apply(lambda x: x.str.strip() if x.dtype=="object" else x)

其中data是任何表的名称


Tags: 数据lambda函数字符串dataifobject错误