Ved Gupta
Ved Gupta

Reputation: 303

Apply several functions to a grouped by dataframe in python

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 This doesn't work.

Upvotes: 0

Views: 81

Answers (1)

tfv
tfv

Reputation: 6259

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

Upvotes: 1

Related Questions