Apply several functions to a grouped by dataframe in python
Question: I have a dataset with some columns which I am using for grouping the database.
There are two more columns; one has dtype object and other is numerical. I
want to find the number of unique values for each group for each column and
also the most common value.

    
    
    # Typo in code next line removed
    df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar','bar','bar',], 'C_object':['str1', 'str2', 'str2', 'str2','str1', 'str1', 'str1', 'str2'], 'D_num': [10, 2, 2, 2, 10, 10, 10, 2]})
    d = df.groupby('A')
    g = d['C_object', 'D_num'].transform(unique)
    

Expected Output [![enter image description
here](http://i.stack.imgur.com/NXW77.jpg)](http://i.stack.imgur.com/NXW77.jpg)
This doesn't work.


Answer: Try this:

    
    
    import pandas as pd
    
    df = pd.DataFrame({'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar','bar','bar',], 'C_object':['str1', 'str2', 'str2', 'str2','str1', 'str1', 'str1', 'str2'], 'D_num': [10, 2, 2, 2, 10, 10, 10, 2]})
    
    df2=pd.DataFrame({'C_object_len_unique': df.groupby('A')['C_object'].apply(lambda x: len(x.unique())), \
                      'C_object_most_common': df.groupby('A')['C_object'].agg(lambda x:x.value_counts().index[0]), \
                      'D_num_len_unique' : df.groupby('A')['D_num'].apply(lambda x: len(x.unique())), \
                      'D_num_most_common': df.groupby('A')['D_num'].agg(lambda x:x.value_counts().index[0]) \
                      }).reset_index()
    print df2
    

