Dataframe 如何填充0';熊猫数据框中有什么?
我想在那些不存在的群集名称中填入0。 正如在预期的输出中一样,我在最后一行添加了0,因为我在dataframe中没有找到任何结果。 输入: 到目前为止我已经试过了Dataframe 如何填充0';熊猫数据框中有什么?,dataframe,Dataframe,我想在那些不存在的群集名称中填入0。 正如在预期的输出中一样,我在最后一行添加了0,因为我在dataframe中没有找到任何结果。 输入: 到目前为止我已经试过了 #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
#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