Python for Data Science by Yuli Vasiliev
Author:Yuli Vasiliev [Vasiliev, Yuli]
Language: eng
Format: epub
ISBN: 9781718502215
Publisher: No Starch Press
Published: 2022-05-16T00:00:00+00:00
Exercise #10: Excluding Total Rows from the DataFrame
Having rows for totals in a DataFrame allows you to use it as a report without having to add further steps. However, if youâre going to use the DataFrame in further aggregation operations, you may need to exclude rows for totals.
Try filtering the df_totals DataFrame created in the previous section, excluding the grand total and subtotal rows. Use the slicing techniques discussed in this chapter.
Selecting All Rows in a Group
In addition to aiding with aggregation, the groupby() function also helps you to select all the rows belonging to a certain group. To accomplish this, objects returned by groupby() provide the get_group() method. Hereâs how it works:
group = df_result.groupby(['Date','Region'])
group.get_group(('2022-02-04','West'))
You group the df_result DataFrame by Date and Region, passing the column names in as a list to groupby(), just like you did previously. Then you invoke the get_group() method on the resulting GroupBy object, passing a tuple with the desired index. This returns the following DataFrame:
Date Region Total
0 2022-02-04 West 87.0
1 2022-02-04 West 112.0
2 2022-02-04 West 20.0
3 2022-02-04 West 24.0
As you can see, the result set isnât an aggregation. Rather, it includes all the order rows related to the specified date and region.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Exploring Deepfakes by Bryan Lyon and Matt Tora(7088)
Robo-Advisor with Python by Aki Ranin(6982)
Offensive Shellcode from Scratch by Rishalin Pillay(5771)
Ego Is the Enemy by Ryan Holiday(4820)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4398)
Microsoft 365 and SharePoint Online Cookbook by Gaurav Mahajan Sudeep Ghatak Nate Chamberlain Scott Brewster(4357)
Python for ArcGIS Pro by Silas Toms Bill Parker(3856)
Elevating React Web Development with Gatsby by Samuel Larsen-Disney(3548)
Machine Learning at Scale with H2O by Gregory Keys | David Whiting(3254)
Learning C# by Developing Games with Unity 2021 by Harrison Ferrone(3233)
Speed Up Your Python with Rust by Maxwell Flitton(3195)
Liar's Poker by Michael Lewis(3177)
OPNsense Beginner to Professional by Julio Cesar Bueno de Camargo(3166)
Extreme DAX by Michiel Rozema & Henk Vlootman(3142)
Agile Security Operations by Hinne Hettema(3098)
Linux Command Line and Shell Scripting Techniques by Vedran Dakic and Jasmin Redzepagic(3087)
Essential Cryptography for JavaScript Developers by Alessandro Segala(3056)
Cryptography Algorithms by Massimo Bertaccini(2970)
AI-Powered Commerce by Andy Pandharikar & Frederik Bussler(2954)
