
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Function Application
Pandas provides powerful methods to apply custom or library functions to DataFrame and Series objects. Depending on whether you want to apply a function to the entire DataFrame, row- or column-wise, or element-wise, Pandas offers several methods to achieve these tasks.
In this tutorial, we will explore three essential methods for function application in Pandas −
- Table wise Function Application: pipe()
- Row or Column Wise Function Application: apply()
- Element wise Function Application: map()
Let's dive into each method and see how they can be utilized effectively.
Table-wise Function Application
The pipe() function allows you to apply chainable functions that expect a DataFrame or Series as input. This method is useful for performing custom operations on the entire DataFrame in a clean and readable manner.
Example: Applying a Custom Function to the Entire DataFrame
Here is the example that demonstrates how you can add a value to all elements in the DataFrame using the pipe() function.
import pandas as pd import numpy as np def adder(ele1,ele2): return ele1+ele2 df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) print('Original DataFrame:\n', df) df.pipe(adder,2) print('Modified DataFrame:\n', df)
Its output is as follows −
Original DataFrame: col1 col2 col3 0 2.349190 1.908931 -0.121444 1 1.306488 -0.946431 0.308926 2 -0.235694 -0.720602 1.089614 3 0.960508 -1.273928 0.943044 4 -1.180202 -0.959529 0.464541 Modified DataFrame: col1 col2 col3 0 2.349190 1.908931 -0.121444 1 1.306488 -0.946431 0.308926 2 -0.235694 -0.720602 1.089614 3 0.960508 -1.273928 0.943044 4 -1.180202 -0.959529 0.464541
Row or Column Wise Function Application
The apply() function is versatile and allows you to apply a function along the axes of a DataFrame. By default, it applies the function column-wise, but you can specify row-wise application using the axis parameter.
Example: Applying a Function Column-wise
This example applies a function to the DataFrame columns. Here the np.mean() function calculates the mean of each column.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3']) print('Original DataFrame:\n', df) result = df.apply(np.mean) print('Result:\n',result)
Its output is as follows −
Original DataFrame: col1 col2 col3 0 -0.024666 0.058480 0.658520 1 -0.040997 1.253245 -1.242394 2 1.073832 -1.039897 0.840698 3 0.248157 -1.985475 0.310767 4 -0.973393 -1.002330 -0.890125 Result: col1 0.056587 col2 -0.543195 col3 -0.064507 dtype: float64
By passing value 1 to the axis parameter, operations can be performed row wise.
Example: Applying a Function Row-wise
This function applies the np.mean() function to the rows of the pandas DataFrame.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3']) print('Original DataFrame:\n', df) result = df.apply(np.mean, axis=1) print('Result:\n',result)
Its output is as follows −
Original DataFrame: col1 col2 col3 0 0.069495 -1.228534 -1.431796 1 0.468724 0.497217 -0.270103 2 -0.754304 0.053360 -1.298396 3 0.762669 -2.181029 -2.067756 4 0.129679 0.131104 1.010851 Result: 0 -0.863612 1 0.231946 2 -0.666446 3 -1.162039 4 0.423878 dtype: float64
Example: Applying a Lambda Function
The following example applies the lambda function to the DataFrame elements using the apply() method.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), columns=['col1', 'col2', 'col3']) print('Original DataFrame:\n', df) result = df.apply(lambda x: x.max() - x.min()) print('Result:\n',result)
Its output is as follows −
Original DataFrame: col1 col2 col3 0 -1.143522 0.413272 0.633881 1 0.200806 -0.050024 0.108580 2 -2.147704 -0.400682 -1.191469 3 2.342222 -2.398639 0.063151 4 -1.071437 1.895879 -0.916805 Result: col1 4.489926 col2 4.294518 col3 1.825350 dtype: float64
Element Wise Function Application
When you need to apply a function to each element individually, you can use map() function. These methods are particularly useful when the function cannot be vectorized.
Example: Using map() Function
The following example demonstrates how to use the map() function for applying a custom function to the elements of the DataFrame object.
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3']) # My custom function df['col1'].map(lambda x:x*100) print(df.apply(np.mean))
Its output is as follows −
col1 0.480742 col2 0.454185 col3 0.266563 dtype: float64