Ad

How To Fix ''Level None Not Found' Error In Pandas?

- 1 answer

Facing following error when running code

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)

I want to take the average value of Threat Score & Score, also count of the user name. Then sort by Threat Score high to low.

Ad

Answer

Its a bug in pandas this is a github link for the same. This error comes with with multiple aggregations per column and margins=True, it won't come if you choose flag margins = False. you can add them later if you want. That sure will work:

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)

let me know if this works for you

Ad
source: stackoverflow.com
Ad