擅长:python、mysql、java
<p>可以使用的方法是<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.itertuples.html?highlight=itertuples#pandas.DataFrame.itertuples" rel="noreferrer">^{<cd1>}</a>,它作为namedtuples在数据帧行上迭代,索引值作为元组的第一个元素。它比<code>iterrows()</code>快得多。对于<code>itertuples()</code>,每个<code>row</code>在数据帧中包含其<code>Index</code>,您可以使用<code>loc</code>来设置该值。</p>
<pre><code>for row in df.itertuples():
if <something>:
df.at[row.Index, 'ifor'] = x
else:
df.at[row.Index, 'ifor'] = x
df.loc[row.Index, 'ifor'] = x
</code></pre>
<p>谢谢@SantiStSupery,<a href="https://stackoverflow.com/questions/37216485/pandas-at-versus-loc">using ^{<cd7>} is much faster</a>。</p>