10:51, 28th May 2021
GroupBy in Pandas: Your Guide to Summarizing and Aggregating Data in Python
The Pandas GroupBy function is a widely used tool in Python's Pandas library that enables analysts and data scientists to organise data into groups based on specific criteria, apply operations to those groups and then combine the results into a meaningful output. Operating on what is known as the Split-Apply-Combine strategy, first introduced by Hadley Wickham in 2011, it breaks large datasets into smaller, more manageable parts before performing calculations and reassembling the findings.
A GroupBy object is created by specifying one or more columns to group by, and from there a range of aggregation functions such as sum, mean, count, median and standard deviation can be applied, either individually or simultaneously using the agg() function. Beyond aggregation, the function also supports transformation, which allows computations to be performed on entire groups before returning a combined dataframe, as well as filtration, which discards values that do not meet defined criteria.
Custom functions can also be applied to grouped results using the apply() method, offering considerable flexibility for handling complex analytical tasks. The ability to group by multiple columns simultaneously and rename aggregated outputs makes it particularly well suited to deriving nuanced insights from structured datasets.
10:33, 28th May 2021
Pandas Groupby: Summarising, Aggregating, Grouping in Python
Python's Pandas library offers a powerful groupby() function that allows users to split large DataFrames into groups based on chosen variables and apply a range of summary statistics to each group. Using a dataset of 830 mobile phone usage records spanning five months, the groupby() function can be combined with agg() to calculate statistics such as sum, mean, min, max and count across grouped data.
The groupby() function returns a GroupBy object, and results can be returned as either a Pandas Series or DataFrame depending on how the operation is structured. Multiple statistics can be calculated per group simultaneously using the agg() function with a dictionary or list of instructions, and custom or lambda functions can also be applied.
Since Pandas version 0.25.0, named aggregations using simple tuples allow grouped columns to be renamed cleanly within a single operation, replacing the older nested dictionary approach that has since been deprecated. When multiple statistics produce a multi-index on columns, methods such as droplevel() or ravel() can be used to simplify and rename the resulting column headers.
10:17, 28th May 2021
Comprehensive Guide to Grouping and Aggregating with Pandas
Grouping and aggregating data is one of the most fundamental analytical operations available in the Pandas library for Python, and the groupby function can be paired with one or more aggregation functions to summarise data quickly and efficiently.
Built-in aggregation options include basic mathematical functions such as sum, mean, median, minimum, maximum, standard deviation and variance, as well as counting functions where it is important to note that count excludes missing values while size does not. Aggregations can be applied using a list, a dictionary or a named aggregation approach, with the dictionary method generally considered the most robust.
Beyond built-in functions, analysts can draw on external libraries such as scipy and numpy, or define their own custom functions using standard definitions, partial functions or lambda expressions, allowing for calculations such as percentile ranges, trimmed means and null value counts. Multiple aggregations across columns can be handled by combining groupby with apply, though this approach is slower and best used sparingly.
Results can be further manipulated through chained groupby operations to produce cumulative totals, and hierarchical column indices that Pandas creates by default can be flattened into single-row labels for easier downstream analysis. For those needing subtotals, the third-party sidetable package provides a straightforward way to add them at multiple levels alongside a grand total.
10:08, 28th May 2021
python-docx is a Python library designed for creating and modifying Microsoft Word (.docx) files. It offers a broad range of functionality, allowing developers to programmatically build documents that include headings, paragraphs, formatted runs of bold and italic content, images, tables, page breaks and various list styles. The library is supported by comprehensive documentation covering a user guide and a detailed API reference, the latter encompassing objects related to documents, styles, paragraphs, tables, sections, comments, shapes and shared utilities such as colour formatting and length handling. A contributor guide and a range of enumerations for alignment, styling and formatting options are also included.
10:07, 28th May 2021
TableOne
Developed by Tom J. Pollard and Alistair E. W. Johnson, TableOne is a Python package designed to simplify the process of generating summary measures from a dataset, making it particularly useful for researchers preparing publications.
10:07, 28th May 2021
5 Methods to Check for NaN values in Python
NaN, which stands for Not A Number, is a special floating-point value used to represent missing data in Python, and identifying a standalone NaN value can be more challenging than locating one within a larger data structure.
There are five commonly used methods for detecting NaN values in Python, three of which rely on built-in functions from popular libraries, specifically the isna() function from pandas, the isnan() function from numpy and the isnan() function from the math library, all of which return True when a NaN value is detected.
The remaining two methods exploit inherent properties of NaN itself, the first being that a NaN value is the only floating-point type that is not equal to itself, and the second being that unlike all other floating-point numbers, NaN does not fall within the range of negative infinity to positive infinity, meaning that any value failing this range check can be identified as NaN.
10:05, 28th May 2021
Downloading files from online sources is a common task in web programming, essential for applications involving file sharing, data collection and retrieving website resources. There are several methods in Python for achieving this, including the urllib.request module's urlretrieve function, which simplifies downloading by requiring only a URL and a local file path.
However, this approach is noted as legacy in Python 3. The urllib2 module, suitable for Python 2, offers similar functionality but requires additional steps to handle file data. The requests library provides a more modern and flexible solution, allowing for easy retrieval of file content and access to HTTP metadata such as status codes and headers.
Additionally, the wget module offers a straightforward one-line method for downloading files without manually opening the destination file. While the author prefers requests for its balance of simplicity and features, alternatives like urllib.request or urllib2 may be necessary depending on project constraints and Python version. Each method is demonstrated with example code, highlighting practical considerations for implementation.
10:04, 28th May 2021
Dask is a Python library designed to facilitate parallel and distributed computing, offering scalable solutions for handling complex data processing tasks. It provides multiple APIs, including Futures for flexible task management, DataFrames for structured data analysis and Arrays and Bags for handling large datasets, enabling users to construct custom workflows and leverage powerful scaling techniques. Installation is straightforward through pip or conda and deployment options range from local setups to cloud and high-performance computing environments. Widely adopted across industries, Dask addresses challenges associated with large-scale data and intensive computations, supporting a variety of applications through extensive documentation, examples and community resources. Its design prioritises usability, performance and adaptability, making it a versatile tool for both individual and collaborative computational projects.
10:03, 28th May 2021
PyTables is a Python package designed to manage hierarchical datasets efficiently, leveraging the HDF5 library and NumPy for handling large volumes of data. It combines an object-oriented interface with performance-optimised C extensions generated via Cython, enabling fast and user-friendly interaction with extensive datasets while minimising memory and disk usage, particularly through on-the-fly compression. The project provides comprehensive documentation, migration guides and resources for users and is supported by the NumFOCUS organisation.
10:02, 28th May 2021
SAS Analysis Explorers is an online platform designed for users of SAS software to engage with a community of peers, access educational resources and participate in challenges that reward progress with tangible benefits. The initiative encourages skill development through tutorials, networking opportunities and interactions with industry experts, allowing participants to share insights and learn from others facing similar data-related challenges.
Users can earn points by completing tasks or milestones, which can be exchanged for items such as technology gadgets, books, or other merchandise. The platform is structured to facilitate exploration of SAS-related content, including updates, events and quizzes, and is accessible to individuals with varying levels of experience. It distinguishes itself from SAS Communities by focusing on user engagement and recognition, offering a space where participants can connect, collaborate and be acknowledged for their contributions to the SAS ecosystem.