wesm / pydata-book

Materials and IPython notebooks for "Python for Data Analysis" by Wes McKinney, published by O'Reilly Media

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ch14 (pg 412) norm_total group function yielding ValueError

robml opened this issue · comments

commented

So in Chapter 14 (the worked through examples) in the first example, page 412, there is a piece of code that normalizes the OS usage of the devices used to shorten gov urls.

The function supplied is as follows:

def norm_total(group):
    group['normed_total'] = group.total / group.total.sum()
    return group

results = count_subset.groupby('tz').apply(norm_total)
sns.barplot(x='normed_total',y='tz',hue='os',data=results)

It is on this last line where I get a ValueError: Could not interpret input 'normed_total'
I haven't had any other issues with apply on groupby before, so I am unsure whether there is a typo or maybe functionality has been deprecated. Any suggestions?

That looks like an error you would get from seaborn if you passed the original count_subset (or something without the normed_total column, or the column name misspelled) -- I tried the example locally with pandas 1.3.5 and it works correctly, here is what the results variable looks like for me in Jupyter:

Screenshot from 2022-01-03 15-50-56