Pandas: what is the data-type of object passed to the agg function
<p>I have been curious about what exactly is passed to the agg function </p>

<pre><code>Id      NAME   SUB_ID
276956  A      5933
276956  B      5934
276956  C      5935
287266  D      1589
</code></pre>

<p>So when I call an agg function, what exactly is the datatype of x.</p>

<pre><code>df.groupby('Id').agg(lambda x: set(x))
</code></pre>

<p>From my own digging up, I find x to be <code>&lt;type 'property'&gt;</code> but I dont understand what exactly it is. What I am trying to do is compress the records into one row for any particular group. So for id 276956 , I want to have A,B,C in one cell under the Name column. I have been doing it by converting it into a set but its causing me some grief with Nan and None values. I was wondering whats the best way to compress in a single row. If these are numpy arrays then I don't really need to convert but something like </p>

<pre><code>df.groupby('Id').agg(lambda x: x)
</code></pre>

<p>throws an error</p>

<pre><code>&gt;&gt;&gt; df[['Id', 'NAME']].groupby('Id').agg(lambda x: ', '.join(x))
           NAME
Id             
276956  A, B, C
287266        D
</code></pre>

<p>The <code>x</code> in this case will be the series for each relevant grouping on <code>Id</code>.</p>

<p>To actually get a list of values:</p>

<pre><code>&gt;&gt;&gt; df[['Id', 'NAME']].groupby('Id').agg(lambda x: x.values.tolist())
             NAME
Id               
276956  [A, B, C]
287266        [D]
</code></pre>

<p>More generally, <code>x</code> will be a dataframe for the relevant grouping and you can perform any action on it that you could normally do with a dataframe, e.g.</p>

<pre><code>&gt;&gt;&gt; df.groupby('Id').agg(lambda x: x.shape)
        NAME SUB_ID
Id                 
276956  (3,)   (3,)
287266  (1,)   (1,)
</code></pre>

python pandas numpy