If order doesnot matter use a reindex and make use of the keys from dict1
:
(df['PII Count'].groupby(df['PII'].map(d_inv)).sum().rename_axis('Cluster names')
.reindex(dict1.keys(),fill_value=0)
.reset_index(name='Total count'))
Cluster names Total count
0 Personal Info 270
1 Health Info 0
2 Network Info 94
3 Others Info 59
4 Finance Info 1
If Order matters:
m = df['PII'].map(d_inv)
out = df['PII Count'].groupby(m).sum()
out = (out.reindex(out.index.union(set(dict1.keys()).difference(m),sort=False),
fill_value=0)
.rename_axis('Cluster names')
.reset_index(name='Total count'))
print(out)
Cluster names Total count
0 Finance Info 1
1 Network Info 94
2 Others Info 59
3 Personal Info 270
4 Health Info 0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…