一些代码即时编写我遇到了问题。
我得到一个错误。 AttributeError:'DataFrame'对象没有属性'str'。运行以下代码时。
BosCle = get_tables('https://www.basketball-reference.com/boxscores/201710170CLE.html')
Get_tables是一个进入网页并获取此网页上的表格的函数。播放的分钟数以min:sec格式给出。我想改变这一点,不知道为什么这不起作用。
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
从您的示例看,BosCle[0]['MP']
不是单个对象,而是DataFrame。如果你更深一层并达到对象str
将工作。