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>

<p>You working with <code>Series</code>:</p>

<pre><code>print (df.groupby('Id').agg(lambda x: print(x)))
0    A
1    B
2    C
Name: NAME, dtype: object
3    D
Name: NAME, dtype: object
0    5933
1    5934
2    5935
Name: SUB_ID, dtype: int64
3    1589
Name: SUB_ID, dtype: int64
</code></pre>

<p>You can working with custom function, but output has to be aggregated:</p>

<pre><code>def f(x):
    print (x)
    return set(x)

print (df.groupby('Id').agg(f))
             NAME              SUB_ID
Id                                   
276956  {C, B, A}  {5933, 5934, 5935}
287266        {D}              {1589}     
</code></pre>

<p>If need aggreagate <code>join</code>, numeric columns are <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#automatic-exclusion-of-nuisance-columns" rel="nofollow">omited</a>:</p>

<pre><code>print (df.groupby('Id').agg(', '.join))
           NAME
Id             
276956  A, B, C
287266        D
</code></pre>

<p>If <code>mean</code>, <code>string</code> columns are omited:</p>

<pre><code>print (df.groupby('Id').mean())
        SUB_ID
Id            
276956    5934
287266    1589
</code></pre>

<hr>

<p>More common is used function <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.apply.html" rel="nofollow"><code>apply</code></a> - see <a href="http://pandas.pydata.org/pandas-docs/stable/groupby.html#flexible-apply" rel="nofollow">flexible apply</a>:</p>

<pre><code>def f(x):
    print (x)
    return ', '.join(x)

print (df.groupby('Id')['NAME'].apply(f))
Id
276956    A, B, C
287266          D
Name: NAME, dtype: object
</code></pre>

python pandas numpy