如何修复pandas中的“Level None not found”错误?

2024-04-19 02:49:35 发布

您现在位置:Python中文网/ 问答频道 /正文

运行代码时遇到以下错误

Level None not found

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

我想取Threat Score&;Score的平均值,也算用户名。然后按Threat Score从高到低排序。你知道吗


Tags: 代码noneptdf错误nptablenot
2条回答
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')

这是熊猫中的一个虫子,这是相同的。此错误伴随着每列的多个聚合和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)

让我知道这是否适合你

相关问题 更多 >