GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing pbpython.com — Pandas Groupby Warning - Practical Business Python
IntroductionOne of the reasons I like using pandas instead of Excel for data analysis is that it is
easier to avoid certain types of copy-paste Excel errors. As great as pandas is, there
is still plenty of opportunity to make errors with pandas code. This article discusses a
subtle issue with pandas The ProblemTo illustrate this problem, we’ll use a simple data set that shows sales for 20 customers and includes their region and an internal customer segment designation of Platinum, Gold or Silver. Here is the full data set:
The data looks pretty simple. There’s only one numeric column so let’s see what it totals to. import pandas as pd df = pd.read_excel('https://github.com/chris1610/pbpython/raw/master/data/sales_9_2022.xlsx') df["Sales"].sum() 8000000
We have $8,000,000 in sales in the spreadsheet. Keep that number in mind. Let’s do some simple analysis to summarize sales by region: df.groupby(['Region']).agg({'Sales': 'sum'})
We can double check the math: df.groupby(['Region']).agg({'Sales': 'sum'}).sum() Sales 8000000 dtype: int64 Looks good. That’s what we expect. Now let’s see what sales look like by Segment: df.groupby(['Region', 'Segment']).agg({'Sales': 'sum'}) Which yields this table:
This looks good. No errors and the table seems reasonable. We should continue our analysis right? Nope. There’s a potentially subtle issue here. Let’s sum the data to double check: df.groupby(['Region', 'Segment']).agg({'Sales': 'sum'}).sum() Sales 7019000 dtype: int64 This only includes $7,019,000. Where did the other $981,000 go? Is pandas broken? You can see the issue clearly if we use the df.groupby(['Region', 'Segment'], dropna=False).agg({'Sales': 'sum'}) Now we can see the
If we check the sum, we can see it totals to $8M. df.groupby(['Region', 'Segment'], dropna=False).agg({'Sales': 'sum'}).sum() Sales 8000000 dtype: int64 The pandas documentation is very clear on this:
The take away is that if your If you are ok dropping those values, then use the default However, if you want to ensure that all values (Sales in this particular case) are included, then
make sure to use An ounce of preventionThe main way to deal with this potential issue is to understand if you have any You can use pure pandas: df.isnull().sum() Customer ID 0 Customer Name 0 Region 0 Segment 4 Sales 0 dtype: int64 There are other tools like missingno which provide a more robust interface for exploring the data. I’m partial to sidetable. Here’s how to use it after it’s installed and imported: df.stb.missing()
Regardless of the approach you use, its worth keeping in mind that you need to know if
you have any null or The other alternative to using the df.fillna('unknown').groupby(['Region', 'Segment']).agg({'Sales': 'sum'}) Now the unknown values are explicitly called out:
ConclusionWhen working with pandas I have been bit by this behavior several times in the past. In some cases, it might not be a big deal. In others, you might need to sheepishly explain why your numbers aren’t adding up. Have you seen this before? Let me know in the comments below.
Tags
pandas
Comments | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |