擅长:python、mysql、java
<p>对于我从另一个问题中剩下的<code>df</code>数据帧:</p>
<pre><code>In [907]: type(df)
Out[907]: pandas.core.frame.DataFrame
In [908]: type(df.loc[1])
Out[908]: pandas.core.series.Series
In [909]: type(df.loc[1][1])
Out[909]: numpy.int64
</code></pre>
<p>查看连续的<code>loc</code>索引步骤如何给出一个<code>Series</code>和一个数值</p>
<p>dataframe的<code>to_numpy</code>方法生成一个2d数组:</p>
<pre><code>In [912]: df.to_numpy().shape
Out[912]: (22, 2)
</code></pre>
<p>应用于该系列,我们得到一个1d阵列:</p>
<pre><code>In [913]: df.loc[1].to_numpy()
Out[913]: array([28, 13])
</code></pre>
<p>但正如你的错误所显示的,就是这样。np.float<code>object does not have a</code>to_numpy<code>method. It already is a</code>numpy`对象</p>
<p>在编写这样的代码时,您应该知道每个步骤生成的对象类型。并准备好测试这些知识。错误的猜测会产生错误</p>