Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

DOC: User Guide Page on user-defined functions #61195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
May 18, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
trim trailing whitespace
  • Loading branch information
arthurlw committed Mar 28, 2025
commit d20bcc735bc39d07a21018527ea3f000e1d17eb0
26 changes: 13 additions & 13 deletions 26 doc/source/user_guide/user_defined_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Introduction to User-Defined Functions
In pandas, User-Defined Functions (UDFs) provide a way to extend the library’s
functionality by allowing users to apply custom computations to their data. While
pandas comes with a set of built-in functions for data manipulation, UDFs offer
flexibility when built-in methods are not sufficient. These functions can be
flexibility when built-in methods are not sufficient. These functions can be
applied at different levels: element-wise, row-wise, column-wise, or group-wise,
and change the data differently, depending on the method used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "change the data differently" sounds very close to mutating in a UDF, which we explicitly do not support. What do you think of "behave differently".

Copy link
Member Author

@arthurlw arthurlw Apr 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Behave differently” sounds clearer and avoids implying mutation. I'll update it!


Expand All @@ -19,13 +19,13 @@ Why Use User-Defined Functions?
Pandas is designed for high-performance data processing, but sometimes your specific
needs go beyond standard aggregation, transformation, or filtering. User-defined functions allow you to:

* **Customize Computations**: Implement logic tailored to your dataset, such as complex
* **Customize Computations**: Implement logic tailored to your dataset, such as complex
transformations, domain-specific calculations, or conditional modifications.
* **Improve Code Readability**: Encapsulate logic into functions rather than writing long,
complex expressions.
* **Handle Complex Grouped Operations**: Perform operations on grouped data that standard
methods do not support.
* **Extend pandas' Functionality**: Apply external libraries or advanced calculations that
* **Extend pandas' Functionality**: Apply external libraries or advanced calculations that
are not natively available.


Expand Down Expand Up @@ -58,14 +58,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe
.. ipython:: python

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# User-Defined Function
def add_one(x):
return x + 1

# Apply function
df_applied = df.apply(add_one)
print(df_applied)
Expand All @@ -81,14 +81,14 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe

# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 2, 3]})

# User-Defined Function
def add_one(x):
return x + 1

def add_two(x):
return x + 2

# Apply function
df_applied = df.apply({"A": add_one, "B": add_two})
print(df_applied)
Expand All @@ -103,11 +103,11 @@ The :meth:`DataFrame.apply` allows applying a user-defined functions along eithe

# Sample Series
s = pd.Series([1, 2, 3])

# User-Defined Function
def add_one(x):
return x + 1

# Apply function
s_applied = s.apply(add_one)
print(s_applied)
Expand All @@ -128,11 +128,11 @@ The :meth:`DataFrame.agg` allows aggregation with a user-defined function along
'Category': ['A', 'A', 'B', 'B'],
'Values': [10, 20, 30, 40]
})

# Define a function for group operations
def group_mean(group):
return group.mean()

# Apply UDF to each group
grouped_result = df.groupby('Category')['Values'].agg(group_mean)
print(grouped_result)
Expand All @@ -149,7 +149,7 @@ transformations and custom row-wise or element-wise operations.
The :meth:`DataFrame.transform` allows transforms a Dataframe, Series or Grouped object
while preserving the original shape of the object.

.. ipython:: python
.. ipython:: python

# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.