使用pandas对数据提取时报错,AttributeError: Can only use .str accessor with string values!

from pandas import DataFrame
from pandas import read_excel
df = read_excel(r'i_nuc.xls',sheet_name='Sheet4')
df.head()

运行结果如下图 

 

 

使用pandas对数据提取时报错,AttributeError: Can only use .str accessor with string values!_第1张图片

 

df.电话.head().str.strip()

结果出现报错

AttributeError: Can only use .str accessor with string values!

这句话翻译成:属性错误:只能使用带有字符串值的 .str 访问器!

解决办法:用astype()转化数据类型

代码如下

df.电话 = df.电话.astype(str)
df.电话.head().str.strip()

使用pandas对数据提取时报错,AttributeError: Can only use .str accessor with string values!_第2张图片 这样便不会报错,运行结果如左图

 

你可能感兴趣的:(pandas,python,数据分析)