<p>要选择<code>ith</code>行<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix" rel="noreferrer">use ^{<cd2>}</a>,请执行以下操作:</p>
<pre><code>In [31]: df_test.iloc[0]
Out[31]:
ATime 1.2
X 2.0
Y 15.0
Z 2.0
Btime 1.2
C 12.0
D 25.0
E 12.0
Name: 0, dtype: float64
</code></pre>
<p>要在<code>Btime</code>列中选择第i个值,可以使用:</p>
<pre><code>In [30]: df_test['Btime'].iloc[0]
Out[30]: 1.2
</code></pre>
<hr/>
<h2><code>df_test['Btime'].iloc[0]</code>(推荐)和<code>df_test.iloc[0]['Btime']</code>之间存在差异:</h2>
<p>数据帧将数据存储在基于列的块中(其中每个块都有一个
数据类型)。如果先选择“按列”,则可以返回一个<em>视图</em>(即
比返回副本更快),并且保留原始数据类型。相反
如果先选择“按行”,并且数据帧中的列具有不同的
数据类型,然后将数据复制到一系列新的对象数据类型中。所以
选择列比选择行快一点。因此,尽管
<code>df_test.iloc[0]['Btime']</code>有效,<code>df_test['Btime'].iloc[0]</code>有点
效率更高</p>
<p>在作业方面,两者有很大的区别。
<code>df_test['Btime'].iloc[0] = x</code>影响<code>df_test</code>,但<code>df_test.iloc[0]['Btime']</code>
可能不会。有关原因的解释,请参见下文。因为一个细微的差别
索引顺序对行为有很大影响,最好使用单个索引分配:</p>
<pre><code>df.iloc[0, df.columns.get_loc('Btime')] = x
</code></pre>
<hr/>
<h2><code>df.iloc[0, df.columns.get_loc('Btime')] = x</code>(推荐):</h2>
<p><strong><a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-view-versus-copy" rel="noreferrer">recommended way</a></strong>将新值分配给
DataFrame是到<a href="https://www.dataquest.io/blog/settingwithcopywarning/" rel="noreferrer">avoid chained indexing</a>,而是使用方法<a href="https://stackoverflow.com/a/32103253/190597">shown by
andrew</a></p>
<pre><code>df.loc[df.index[n], 'Btime'] = x
</code></pre>
<p>或</p>
<pre><code>df.iloc[n, df.columns.get_loc('Btime')] = x
</code></pre>
<p>后一种方法要快一点,因为<code>df.loc</code>必须将行和列标签转换为
位置索引,因此如果使用
<code>df.iloc</code></p>
<hr/>
<h2><code>df['Btime'].iloc[0] = x</code>有效,但不建议:</h2>
<p>虽然这是可行的,但它利用了当前<em>实现数据帧的方式。不能保证熊猫将来会以这种方式工作。特别是,它利用了(当前)<code>df['Btime']</code>总是返回
视图(不是副本),因此<code>df['Btime'].iloc[n] = x</code>可用于<em>分配</em>一个新值
在<code>df</code>的<code>Btime</code>列的第n个位置</p>
<p>由于Pandas没有明确保证索引器何时返回视图而不是副本,因此使用链式索引的分配通常会引发<code>SettingWithCopyWarning</code>,即使在这种情况下,分配成功地修改了<code>df</code>:</p>
<pre><code>In [22]: df = pd.DataFrame({'foo':list('ABC')}, index=[0,2,1])
In [24]: df['bar'] = 100
In [25]: df['bar'].iloc[0] = 99
/home/unutbu/data/binky/bin/ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
In [26]: df
Out[26]:
foo bar
0 A 99 <-- assignment succeeded
2 B 100
1 C 100
</code></pre>
<hr/>
<h2><code>df.iloc[0]['Btime'] = x</code>不起作用:</h2>
<p>相反,使用<code>df.iloc[0]['bar'] = 123</code>的赋值不起作用,因为<code>df.iloc[0]</code>正在返回副本:</p>
<pre><code>In [66]: df.iloc[0]['bar'] = 123
/home/unutbu/data/binky/bin/ipython:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
In [67]: df
Out[67]:
foo bar
0 A 99 <-- assignment failed
2 B 100
1 C 100
</code></pre>
<hr/>
<p><strong>警告</strong>:我以前建议<code>df_test.ix[i, 'Btime']</code>。但这不能保证为您提供<code>ith</code>值,因为<code>ix</code>在尝试按<em>位置</em>索引之前,会尝试按<em>标签</em>索引。因此,如果数据帧的整数索引不是从0开始排序的,那么使用<code>ix[i]</code>将返回标记为</em><code>i</code>的行<em>,而不是<code>ith</code>行。比如说,</p>
<pre><code>In [1]: df = pd.DataFrame({'foo':list('ABC')}, index=[0,2,1])
In [2]: df
Out[2]:
foo
0 A
2 B
1 C
In [4]: df.ix[1, 'foo']
Out[4]: 'C'
</code></pre>