<p>您可以使用<code>ngroup</code>的组合,然后计算其值:</p>
<pre><code># create a ID variable with a unique identifier for each group
groupvar = df.groupby(['l_name', 'f_name', 'city_state', 'zip']).ngroup(ascending=False) + 1
# assign it to the data
df = df.assign(tag=groupvar)
df
l_name f_name m_name city_state zip tag
0 smith sam auburn,wa 98910 2
1 smith sam c auburn,wa 98910 2
2 smith sam durham,nc 27659 1
3 smith sam eddie auburn,wa 98910 2
4 smith sam auburn,wa 98910 2
5 clair may seattle,wa 98092 3
6 clair may a seattle,wa 98092 3
7 clair may seattle,wa 98092 3
8 aaron joe chris ocala,fl 58910 4
9 aaron joe ocala,fl 58910 4
10 aaron joe durham,nc 27659 5
11 aaron joe ocala,fl 58910 4
12 aaron joe a ocala,fl 58910 4
# determine which IDs occur only once and assign 0 to them
df.loc[df['tag'].value_counts()[df['tag']].values == 1, 'tag'] = 0
df
l_name f_name m_name city_state zip tag
0 smith sam auburn,wa 98910 2
1 smith sam c auburn,wa 98910 2
2 smith sam durham,nc 27659 0
3 smith sam eddie auburn,wa 98910 2
4 smith sam auburn,wa 98910 2
5 clair may seattle,wa 98092 3
6 clair may a seattle,wa 98092 3
7 clair may seattle,wa 98092 3
8 aaron joe chris ocala,fl 58910 4
9 aaron joe ocala,fl 58910 4
10 aaron joe durham,nc 27659 0
11 aaron joe ocala,fl 58910 4
12 aaron joe a ocala,fl 58910 4
</code></pre>