Traceback (most recent call last): File "C:/Users/DELL/Desktop/python_flask-dc/python_flask/bpnn_tf.py", line 106, in <module> last_three = column.str[-3:] File "E:\python-3.7.0\lib\site-packages\pandas\core\generic.py", line 5137, in __getattr__ return object.__getattribute__(self, name) File "E:\python-3.7.0\lib\site-packages\pandas\core\accessor.py", line 187, in __get__ accessor_obj = self._accessor(obj) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2100, in __init__ self._inferred_dtype = self._validate(data) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2157, in _validate raise AttributeError("Can only use .str accessor with string values!") AttributeError: Can only use .str accessor with string values!Traceback (most recent call last): File "C:/Users/DELL/Desktop/python_flask-dc/python_flask/bpnn_tf.py", line 106, in <module> last_three = column.str[-3:] File "E:\python-3.7.0\lib\site-packages\pandas\core\generic.py", line 5137, in __getattr__ return object.__getattribute__(self, name) File "E:\python-3.7.0\lib\site-packages\pandas\core\accessor.py", line 187, in __get__ accessor_obj = self._accessor(obj) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2100, in __init__ self._inferred_dtype = self._validate(data) File "E:\python-3.7.0\lib\site-packages\pandas\core\strings.py", line 2157, in _validate raise AttributeError("Can only use .str accessor with string values!") AttributeError: Can only use .str accessor with string values!
时间: 2023-07-24 18:50:31 浏览: 173
Mac下anaconda中Spyder打开报错Traceback(most recent call last)…问题
5星 · 资源好评率100%
这个错误信息是由于你使用了`pandas`库中的`str`属性,但是该属性只能用于字符串类型的数据,而你传入的数据类型不是字符串类型,导致报错。
解决方法:
1. 检查你传入的数据类型是否正确。如果不是字符串类型,则需要将其转换为字符串类型。
2. 如果你的数据类型是`pandas`的`Series`类型,可以使用`astype()`函数将其转换为字符串类型,例如:
``` python
import pandas as pd
# 将数字类型的Series转换为字符串类型
s = pd.Series([1, 2, 3])
s = s.astype(str)
```
3. 如果你的数据类型是`pandas`的`DataFrame`类型,可以使用`apply()`函数将其每一列转换为字符串类型,例如:
``` python
import pandas as pd
# 将数字类型的DataFrame每一列转换为字符串类型
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df = df.apply(lambda x: x.astype(str))
```
4. 如果你的数据类型是列表类型,则可以使用列表推导式将其转换为字符串类型,例如:
``` python
# 将数字类型的列表转换为字符串类型
a = [1, 2, 3]
a = [str(i) for i in a]
```
阅读全文