Question: I have the following code and I want for every `H2` with `id` attribute I would create an anchor link. I can not get it to work properly...

```
<div id="mydiv">
    <h2 id="first">first</h2>
    <h2>second</h2>
    <h2 id="third">third</h2>
</div>

.sls {color: red;font-size: 12px;}

$('#mydiv')
.find('h2[id]')
.append(' <a href="http://www.test.com/page.html#'+ $('h2[id]')
.attr('id') +'" class="sls">Link</a>');

```

**<https://jsfiddle.net/ivanionut/1ohh2hws/>**

Thanks for your help.

Answer: 
You can use `groupby.transform` with `nunique()` function to count the number of unique elements per *id*:

```
df['nos'] = df.groupby("id")['acct_nos'].transform("nunique")
df

```

[![enter image description here](https://i.stack.imgur.com/be9ri.png)](https://i.stack.imgur.com/be9ri.png)
***option 1*** 

```
df.assign(nos=df.id.map(df.drop_duplicates(['id', 'acct_nos']).id.value_counts()))

```

***option 2***  

Using `Counter`

```
from collections import Counter

tups = pd.unique(
    zip(df.id.values.tolist(), df.acct_nos.values.tolist())
).tolist()
df.assign(nos=df.id.map(Counter([tup[0] for tup in tups])))

   id acct_nos   name  nos
0   1       1a    one    1
1   1       1a    two    1
2   2       2b  three    1
3   3       3a   four    4
4   3       3b   five    4
5   3       3c    six    4
6   3       3d  seven    4

```