I have data of the following form:
df = pd.DataFrame({
\'group\': [1, 1, 2, 3, 3, 3, 4],
\'param\': [\'a\', \'a\', \'b\', np.nan, \'a\', \'a\', np.na
I think you can use SeriesGroupBy.nunique:
print (df.groupby('param')['group'].nunique())
param
a 2
b 1
Name: group, dtype: int64
Another solution with unique, then create new df by DataFrame.from_records, reshape to Series by stack and last value_counts:
a = df[df.param.notnull()].groupby('group')['param'].unique()
print (pd.DataFrame.from_records(a.values.tolist()).stack().value_counts())
a 2
b 1
dtype: int64