Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the issue is that there are two different <code>first</code> methods which share a name but act differently, one is for <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#splitting-an-object-into-groups" rel="noreferrer">groupby objects</a> and <a href="http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.first.html" rel="noreferrer">another for a Series/DataFrame</a> (to do with timeseries).</p> <p>To replicate the behaviour of the groupby <code>first</code> method over a DataFrame using <code>agg</code> you could use <code>iloc[0]</code> (which gets the first row in each group (DataFrame/Series) by index):</p> <pre><code>grouped.agg(lambda x: x.iloc[0]) </code></pre> <p>For example:</p> <pre><code>In [1]: df = pd.DataFrame([[1, 2], [3, 4]]) In [2]: g = df.groupby(0) In [3]: g.first() Out[3]: 1 0 1 2 3 4 In [4]: g.agg(lambda x: x.iloc[0]) Out[4]: 1 0 1 2 3 4 </code></pre> <p><em>Analogously you can replicate <code>last</code> using <code>iloc[-1]</code>.</em></p> <p>Note: This will works column-wise, et al:</p> <pre><code>g.agg({1: lambda x: x.iloc[0]}) </code></pre> <p><em>In older version of pandas you could would use the irow method (e.g. <code>x.irow(0)</code>, see previous edits.</em></p> <hr> <p>A couple of updated notes:</p> <p>This is better done using the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.nth.html" rel="noreferrer"><code>nth</code></a> groupby method, which is much faster >=0.13:</p> <pre><code>g.nth(0) # first g.nth(-1) # last </code></pre> <p><em>You have to take care a little, as the default behaviour for <code>first</code> and <code>last</code> ignores NaN rows... and IIRC for DataFrame groupbys it was broken pre-0.13... there's a <code>dropna</code> option for <code>nth</code>.</em></p> <p>You can use the strings rather than built-ins (though IIRC pandas spots it's the <code>sum</code> builtin and applies <code>np.sum</code>):</p> <pre><code>grouped['D'].agg({'result1' : "sum", 'result2' : "mean"}) </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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