‘DataFrame’ object has no attribute ‘dtype’或者’DataFrame’ object has no attribute ‘str’
使用pandas学习数据分析时报错
原因:使用函数str.strip()去除空格和函数dtype()查看数据类型时,只能一次对一列进行操作。
解决:使用循环语句。
df1 = df.copy() #复制一份文件,防止意外
a1 = list(df.columns)
df[a1]= df1[a1].astype(str) #转换为str类型
for i in a1:
df[i] = df[i].str.strip() #去除两边空格