<p>因此,在用伪造的值伪造我自己的数据帧并尝试解决这些问题之后,我发现我遇到了一个<code>ValueError: setting an array element with a sequence</code>。这是因为您将一个列表附加到一个列表中,并试图将其用作df中的列索引:</p>
<pre><code>numlist = ['PlayerLevel']
denomList = ['TeamName', 'Year']
numlist.append(denomlist) # as you suspected this is problematic:
print(numlist)
['PlayerLevel', ['TeamName', 'Year']]
</code></pre>
<p>试试这个:</p>
^{pr2}$
<p>{cd2>的某个地方提供了一个完整的<cd2>子句?在任何情况下,如果这不能解决您的问题,请提供您的数据帧的一个小版本。在</p>
<hr/>
<p><strong>编辑:</strong>来自<a href="https://docs.python.org/3/tutorial/errors.html#handling-exceptions" rel="nofollow">docs on exceptions</a>:“最后一个except子句可以省略异常名,作为通配符。使用时要格外小心,因为这样很容易掩盖真正的编程错误!”在</p>
<p>当然,看看链接的文档,但这里是直接问题的要点。这样写try/except子句被认为是不好的做法:</p>
<pre><code>try:
# do stuff
except:
# do different/more stuff if original stuff fails
</code></pre>
<p>因为<code>except:</code>除了<em>所有</em>类型的<code>Exceptions</code>。为了模仿引用的文献,<strong>这看起来好像没有错误。</strong>此外,正如这个问题的例子,<strong>这使得我们不可能知道到底是什么导致了错误</strong>(如果你甚至设法发现了一个错误)。在几乎所有情况下,您都应该对代码可能引发的错误有一个预期,因此try/except应该如下所示:</p>
<pre><code>try:
# do stuff here
except ValueError: # or whatever type of child of Exception()
# do different/more stuff if original stuff fails
</code></pre>
<p>如果出于某种疯狂的原因,</strong>必须执行通配符<code>except:</code>,那么最好进行重构,这样就没有必要了,但至少,<code>print()</code><em>某种指示^{<cd7>失败的</em>类型的消息。在</p>
<p>一般来说,为了避免这个问题(因为它涉及到通配符或甚至是特定的异常),<strong>尽最大努力确保try/except子句只包含完成目标所需的代码。</strong></p>