擅长:python、mysql、java
<p>据我所知,你的问题似乎与你在结果中得到<code>NaN</code>有关。在</p>
<p>这样做的原因是,您试图在一个数据帧与另一个数据帧之间<code>.append()</code>,而它们没有相同的列。在</p>
<p><code>df2</code>有一个额外的列,即用<code>apply()</code>和{<cd6>}创建的列,而{<cd7>}没有该列。当尝试<code>append</code>一个<code>pd.DataFrame</code>到另一个时,结果将有所有列都是<code>pd.DataFrame</code>对象。当然,由于<code>df1</code>中不存在此列,因此您将为<code>['specialSauce']</code>提供一些<code>NaN</code>。在</p>
<p>如果使用<code>pd.concat()</code>,这是相同的,在本例中,两个方法都执行相同的操作。为了使结果更接近所需的结果,您可以使用<code>ignore_index</code>标志,如下所示:</p>
<pre><code>>> df2.append(df1, ignore_index=True)
</code></pre>
<p>这至少会为结果<code>pd.DataFrame</code>提供一个“fresh”索引。在</p>
<p><strong>编辑</strong></p>
<p>如果您要寻找的是以新列(<code>['specialSauce']</code>)的形式将<code>doStuff</code>的结果“追加”到现有的<code>df</code>的末尾,那么您需要做的是使用<code>pd.concat()</code>,如下所示:</p>
^{pr2}$
<p>这将返回您想要的结果<code>pd.DataFrame</code>。在</p>
<p>如果您有一个<code>pd.Series</code>添加到<code>df1</code>的列中,那么您需要这样添加它:</p>
^{3}$
<p>我希望这有帮助,如果没有,请重新描述一下你所追求的。在</p>