Question: Is there a more pythonic (i.e. no `for` loop) way to produce the `count` column in the below dataframe?

```
import pandas as pd

val = [1, 1, 1, 1, 2, 3, 4, 4, 5, 5, 5, 6, 7, 7, 7, 7]

df = pd.DataFrame(val, index = pd.date_range("2021-05-30", freq="D",
                               periods=len(val)), columns=['vals'])

df['count'] = 0

for n in set(df['vals']):
     end = df['vals'].value_counts()[n] + 1
     df.loc[df['vals'] == n, 'count'] = range(1, end)

df
            vals  count
2021-05-30     1      1
2021-05-31     1      2
2021-06-01     1      3
2021-06-02     1      4
2021-06-03     2      1
2021-06-04     3      1
2021-06-05     4      1
2021-06-06     4      2
2021-06-07     5      1
2021-06-08     5      2
2021-06-09     5      3
2021-06-10     6      1
2021-06-11     7      1
2021-06-12     7      2
2021-06-13     7      3
2021-06-14     7      4

```

Answer: 
One way:

```
df['count'] = df.groupby('vals').cumcount() + 1

```

### OUTPUT:

```
            vals  count
2021-05-30     1      1
2021-05-31     1      2
2021-06-01     1      3
2021-06-02     1      4
2021-06-03     2      1
2021-06-04     3      1
2021-06-05     4      1
2021-06-06     4      2
2021-06-07     5      1
2021-06-08     5      2
2021-06-09     5      3
2021-06-10     6      1
2021-06-11     7      1
2021-06-12     7      2
2021-06-13     7      3
2021-06-14     7      4

```
Try:

```
df['count']=df.groupby((df['vals'] != df['vals'].shift()).cumsum()).cumcount() + 1

```

Output:

```
            vals  count
2021-05-30     1      1
2021-05-31     1      2
2021-06-01     1      3
2021-06-02     1      4
2021-06-03     2      1
2021-06-04     3      1
2021-06-05     4      1
2021-06-06     4      2
2021-06-07     5      1
2021-06-08     5      2
2021-06-09     5      3
2021-06-10     6      1
2021-06-11     7      1
2021-06-12     7      2
2021-06-13     7      3
2021-06-14     7      4

```

This method will hand duplicate vals that are not contiguous restarting the count for each change in vals.