I have a dataframe with date
and cat_1
variables among other variables. I want to get number of unique values of cat_1
, per date. Here is an example: Below is a toy dataframe.
date foo
0 2019-04-21 a
1 2019-04-21 b
2 2019-04-23 c
3 2019-05-01 c
4 2019-05-01 c
5 2019-04-23 a
I want to get is the following:
2019-04-21 2
2019-04-23 2
2019-05-01 1
I got it, but I think it is a bit convoluted. My code:
a.groupby(by='date')['foo'].apply(np.unique).apply(len)
Is there any other straightforward way to get the same answer?
Thanks