如何解决'列标签'Avg_Threat_Score'不唯一.'? pandas 问题 [英] How to solve 'The column label 'Avg_Threat_Score' is not unique.'? issue in pandas

查看:109
本文介绍了如何解决'列标签'Avg_Threat_Score'不唯一.'? pandas 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行代码时,我面临以下错误.错误 - 列标签Avg_Threat_Score"不是唯一的.

When running the code I am facing following error. error - The column label 'Avg_Threat_Score' is not unique.

我正在创建一个数据透视表并希望将值从高到低排序.

I was creating a pivot table and wanted to sort the value from high to low.

pt = df.pivot_table(index = 'User Name',values = ['Threat Score', 'Score'], 
        aggfunc = {
                   'Threat Score': np.mean,
                   'Score' :[np.mean, lambda x: len(x.dropna())]
                  }, 
        margins = False) 

new_col =['User Name Count', 'AVG_TH_Score', 'Avg_Threat_Score']
pt.columns = [new_col]
#befor this code is working, after that now working 
df = df.reindex(pt.sort_values
                    (by = 'Avg_Threat_Score',ascending=False).index)

需要对列 'Avg_Threat_Score' 的值进行高低排序

need to sort the values high low for column 'Avg_Threat_Score'

推荐答案

pt = df.pivot_table(index = 'User Name', values = ['Threat Score', 'Score','Source IP'] ,  
                    aggfunc = {"Source IP" : 'count',
                              'Threat Score':np.mean,
                               'Score': np.mean})

pt = pt.sort_values('Threat Score', ascending = False) 
new_cols = ['Avg_Score', 'Count', 'Avg_ThreatScore']
pt.columns = new_cols

这篇关于如何解决'列标签'Avg_Threat_Score'不唯一.'? pandas 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆