存在しないクラスター名に0を入力したいと思います。期待される出力と同様に、データフレームでその結果が見つからなかったため、最後の行に0を追加しました。入力:
私はこれまで試しました
#I have made clusters according to the requirement and making sum of it
# output of this code is given above
d_inv = {x: k for k, v in dict1.items() for x in v}
df = df['PII Count'].groupby(df['PII'].map(d_inv)).sum() \
.rename_axis('Cluster names') \
.reset_index(name='Total count')
print(df)
順序が重要でない場合は、インデックスの再作成を使用し、次のキーを使用します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
注文が重要な場合:
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加