Note that there are some explanatory texts on larger screens.

plurals
  1. POIndexing a pandas dataframe by integer
    primarykey
    data
    text
    <p>I can't seem to find an elegant way to <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html" rel="nofollow noreferrer">index</a> a <a href="http://pandas.pydata.org/pandas-docs/stable/dsintro.html" rel="nofollow noreferrer"><code>pandas.DataFrame</code></a> by an integer index. In the following example I want to get the value 'a' from the first element of the <code>'A'</code> column. </p> <pre><code>import pandas df = pandas.DataFrame( {'A':['a','b', 'c'], 'B':['f', 'g', 'h']}, index=[10,20,30] ) </code></pre> <p>I would expect <code>df['A'].ix[0]</code> and <code>df['A'][10]</code> both to return <code>'a'</code>. The <code>df['A'][10]</code> does return <code>'a'</code>, but <code>df['A'].ix[0]</code> throws a <code>KeyError: 0</code>. The only way I could think of to get the value <code>'a'</code> based on the index 0 is to use the following approach. </p> <pre><code>df['A'][df['A'].index[0]] </code></pre> <p>Is there a shorter way to get <code>'a'</code> out of the dataframe, using the 0 index?</p> <p><strong>Update</strong></p> <p>As of pandas 0.11 there is a another way to <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-integer" rel="nofollow noreferrer">index by integer</a>.</p> <pre><code>df.iloc[0] # integer based, gives the first row df.loc[10] # label based, gives the row with label 10 </code></pre> <p>This <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#deprecations" rel="nofollow noreferrer">supersedes</a> the <code>irow</code> approach . </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload