How to solve 'The column label 'Avg_Threat_Score' is not unique.'? suing pandas

微笑、不失礼 提交于 2020-06-28 05:53:11

问题


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)

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


回答1:


You need pass new columns names by list, not by nested list, because pandas create MultiIndex with one level.

new_col =['User Name Count', 'AVG_TH_Score', 'Avg_Threat_Score']
pt.columns = [new_col]

Is same like:

pt.columns = [['User Name Count', 'AVG_TH_Score', 'Avg_Threat_Score']]

ValueError: The column label 'Avg_Threat_Score' is not unique.
For a multi-index, the label must be a tuple with elements corresponding to each level.

So use:

pt.columns = ['User Name Count', 'AVG_TH_Score', 'Avg_Threat_Score']

Sample:

df = pd.DataFrame({
        'User Name':list('ababaa'),
         'Threat Score':[4,5,4,np.nan,5,4],
         'Score':[np.nan,8,9,4,2,np.nan],
         'D':[1,3,5,7,1,0]})

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))

pt.columns = ['User Name Count', 'AVG_TH_Score', 'Avg_Threat_Score']
print (pt)
           User Name Count  AVG_TH_Score  Avg_Threat_Score
User Name                                                 
a                      2.0           5.5              4.25
b                      2.0           6.0              5.00

And then for sorting by ordering from Avg_Threat_Score use ordered Categorical for column User Name, so last sort_values working:

names = pt.sort_values(by = 'Avg_Threat_Score',ascending=False).index
print (names)
#Index(['b', 'a'], dtype='object', name='User Name')

df['User Name'] = pd.CategoricalIndex(df['User Name'], categories=names, ordered=True)
df = df.sort_values('User Name')

print (df)
  User Name  Threat Score  Score  D
1         b           5.0    8.0  3
3         b           NaN    4.0  7
0         a           4.0    NaN  1
2         a           4.0    9.0  5
4         a           5.0    2.0  1
5         a           4.0    NaN  0



回答2:


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


来源:https://stackoverflow.com/questions/56312312/how-to-solve-the-column-label-avg-threat-score-is-not-unique-suing-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!