如何修复Pandas中的“未找到级别”错误?

时间:2019-05-25 09:56:59

标签: python pandas

运行代码时遇到以下错误

n = int(input('Enter the number of elements: ')) print('Enter the elements: ') arr = list(set(int(input()) for _ in range(n))) arr.sort() print(arr)

Enter the number of elements: 6
Enter the elements:
4
2
2
3
1
3
[1, 2, 3, 4]

我想取Level None not foundpt = df.pivot_table(index = 'User Name',values = ['Threat Score', 'Score'], aggfunc = { 'Threat Score': np.mean, 'Score' :[np.mean, lambda x: len(x.dropna())] }, margins = True) pt = pt.sort_values('Score', ascending = False) 的平均值,还要计算用户名。然后按Threat Score从高到低排序。

2 个答案:

答案 0 :(得分:1)

它是熊猫中的虫子,这同样是github link。此错误是每个列和margins=True都有多个聚合的结果,如果选择标志margins = False则不会出现此错误。您可以稍后添加它们。那肯定会起作用:

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 = pt.sort_values('Score', ascending = False)

让我知道这是否适合您

答案 1 :(得分:0)

pt = df.pivot_table(index = 'User Agent', 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
pt.to_csv(Path3 + '\\AllUserAgent.csv')