|
| 1 | +.. _user_defined_functions: |
| 2 | + |
| 3 | +{{ header }} |
| 4 | + |
| 5 | +************************************** |
| 6 | +Introduction to User Defined Functions |
| 7 | +************************************** |
| 8 | + |
| 9 | +In pandas, User Defined Functions (UDFs) provide a way to extend the library’s |
| 10 | +functionality by allowing users to apply custom computations to their data. While |
| 11 | +pandas comes with a set of built-in functions for data manipulation, UDFs offer |
| 12 | +flexibility when built-in methods are not sufficient. These functions can be |
| 13 | +applied at different levels: element-wise, row-wise, column-wise, or group-wise, |
| 14 | +depending on the method used. |
| 15 | + |
| 16 | +Note: User Defined Functions will be abbreviated to UDFs throughout this guide. |
| 17 | + |
| 18 | +Why Use UDFs? |
| 19 | +------------- |
| 20 | + |
| 21 | +Pandas is designed for high-performance data processing, but sometimes your specific |
| 22 | +needs go beyond standard aggregation, transformation, or filtering. UDFs allow you to: |
| 23 | +* Customize Computations: Implement logic tailored to your dataset, such as complex |
| 24 | + transformations, domain-specific calculations, or conditional modifications. |
| 25 | +* Improve Code Readability: Encapsulate logic into functions rather than writing long, |
| 26 | + complex expressions. |
| 27 | +* Handle Complex Grouped Operations: Perform operations on grouped data that standard |
| 28 | + methods do not support. |
| 29 | +* Extend pandas' Functionality: Apply external libraries or advanced calculations that |
| 30 | + are not natively available. |
| 31 | + |
| 32 | + |
| 33 | +Where Can UDFs Be Used? |
| 34 | +----------------------- |
| 35 | + |
| 36 | +UDFs can be applied across various pandas methods that work with both Series and DataFrames: |
| 37 | + |
| 38 | +* :meth:`DataFrame.apply` - A flexible method that allows applying a function to Series, |
| 39 | + DataFrames, or groups of data. |
| 40 | +* :meth:`DataFrame.agg` (Aggregate) - Used for summarizing data, supporting multiple |
| 41 | + aggregation functions. |
| 42 | +* :meth:`DataFrame.transform` - Applies a function to groups while preserving the shape of |
| 43 | + the original data. |
| 44 | +* :meth:`DataFrame.filter` - Filters groups based on a function returning a Boolean condition. |
| 45 | +* :meth:`DataFrame.map` - Applies an element-wise function to a Series, useful for |
| 46 | + transforming individual values. |
| 47 | +* :meth:`DataFrame.pipe` - Allows chaining custom functions to process entire DataFrames or |
| 48 | + Series in a clean, readable manner. |
| 49 | + |
| 50 | +Each of these methods can be used with both Series and DataFrame objects, providing versatile |
| 51 | +ways to apply user-defined functions across different pandas data structures. |
0 commit comments