我试图使用以下脚本将列表中的所有值转换为小写:
def Data_Cleanse(Data_IMP):
# Import Data_IMP file
# Drop the EventTime, and EventID columns
Data_Cleanse = Data_IMP.drop(columns = ["eventId","eventTime"])
res = [sub.replace('.', ' - ') for sub in Data_Cleanse]
res = [sub.replace("organization", "organisation") for sub in res]
for i in res:
res = res.str.lower[i]
Data_Cleanse.columns = res
return Data_Cleanse脚本在到达for循环之前运行良好。当达到这一点时,我会得到以下错误:
AttributeError:'list‘对象没有属性'str’
res输出文件的一个示例如下所示:
'addrDate',‘出生地’,‘生日’,‘诞生地’,‘分支’,‘经纪代码’,‘商业邮件’,'businessPhoneNumber‘
关于如何避免这个错误,有什么建议吗?
发布于 2019-11-25 17:15:52
您的原始错误可能是因为您试图在要在pandas.Series对象上运行的列表上运行代码。实际上,通过将其中的一些字符串操作链接到一个列表理解中,您可以简化脚本。类似于:
res = [sub.replace('.', ' - ').replace("organization", "organisation").lower() for sub in Data_Cleanse]发布于 2019-11-25 17:11:06
这是不正确的,这不是Python中列表的工作方式:
for i in res:
res = res.str.lower[i]我相信你是有意这么做的:
for i in range(len(res)):
res[i] = res[i].lower()或者更好的是,这个-使用列表理解:
res = [s.lower() for s in res]https://stackoverflow.com/questions/59036856
复制相似问题