【发布时间】:2018-09-12 19:11:27
【问题描述】:
我在编写一些代码时遇到了问题。
我遇到了一个错误。 AttributeError:“DataFrame”对象没有属性“str”。运行以下代码时。
BosCle = get_tables('https://www.basketball-reference.com/boxscores/201710170CLE.html')
Get_tables 是一个进入网页并获取该网页上的表格的功能。播放的分钟数以分钟:秒格式给出。 我想改变这个,不知道为什么这不起作用。
BosCle[0]['MP'].dtypes
OUTPUT: MP object
MP object
dtypes: object
然后我尝试使用以下代码拆分字符串。
BosCle[0]['MP'].str.split(':')
我尝试了 str.replace 或 str.split,这也是可行的选择,但它给出了相同的错误:
AttributeError Traceback (most recent call last)
<ipython-input-15-802e713eb38b> in <module>()
----> 1 BosCle[0]['MP'].str.split(':')
~\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
4370 if self._info_axis._can_hold_identifiers_and_holds_name(name):
4371 return self[name]
-> 4372 return object.__getattribute__(self, name)
4373
4374 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'str'
希望有人可以提供帮助。
这是表格中的数据。
Starters Team Opponent Away/Home MP FG FGA FG%
Player 1 BOS CLE A 39:12 3 5 .6
Player 2 BOS CLE A 32:31 2 2 1.
Player 3 BOS CLE A 31:53 1 5 .2
Player 4 BOS CLE A 29:01 5 5 1
Player 5 BOS CLE A 25:11 6 10 .6
Player 6 BOS CLE A 7:12 1 5 .2
Player 7 BOS CLE A 9:12 0 5 0
Player 8 BOS CLE A 0 0 0 0
Player 9 BOS CLE A 0 0 0 0
【问题讨论】:
-
如果数据类型是对象,为什么它们是可行的选择?我错过了什么吗?
-
能否也添加一些虚拟数据?
-
您是否希望数据框具有属性
str?为什么? -
报错说
BosCle[0]['MP']上没有属性str,所以不能做BosCle[0]['MP'].str。 -
是的,我也可以尝试添加表格。
标签: python error-handling attributes