
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Apply function to every row in a Pandas DataFrame in Python
In this tutorial, we are going to learn about the most common methods of a list i.e.., append() and extend(). Let's see them one by one.
apply()
It is used to apply a function to every row of a DataFrame. For example, if we want to multiply all the numbers from each and add it as a new column, then apply() method is beneficial. Let's see different ways to achieve it.
Example
# importing the pandas package import pandas as pd # function to multiply def multiply(x, y): return x * y # creating a dictionary for DataFrame data = { 'Maths': [10, 34, 53], 'Programming': [23, 12, 43] } # creating DataFrame using the data data_frame = pd.DataFrame(data) # displaying DataFrame print('--------------------Before------------------') print(data_frame) print() # applying the function multiply data_frame['Multiply'] = data_frame.apply(lambda row : multiply(row['Maths'], row[' Programming']), axis = 1) # displaying DataFrame print('--------------------After------------------') print(data_frame)
Output
If you run the above program, you will get the following results.
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Multiply 0 10 23 230 1 34 12 408 2 53 43 2279
Example
We can also use predefined functions like sum, pow, etc..,
# importing the pandas package import pandas as pd # creating a dictionary for DataFrame data = { 'Maths': [10, 34, 53], 'Programming': [23, 12, 43] } # creating DataFrame using the data data_frame = pd.DataFrame(data) # displaying DataFrame print('--------------------Before------------------') print(data_frame) print() # applying the function multiply # using built-in sum function data_frame['Multiply'] = data_frame.apply(sum, axis = 1) # displaying DataFrame print('--------------------After------------------') print(data_frame)
Output
If you run the above program, you will get the following results.
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Multiply 0 10 23 33 1 34 12 46 2 53 43 96
Example
We can also use functions from the numpy module. Let's see one example.
# importing the pandas package import pandas as pd # importing numpy module for functions import numpy as np # creating a dictionary for DataFrame data = { 'Maths': [10, 34, 53], 'Programming': [23, 12, 43] } # creating DataFrame using the data data_frame = pd.DataFrame(data) # displaying DataFrame print('--------------------Before------------------') print(data_frame) print() # applying the function multiply # using sum function from the numpy module data_frame['Multiply'] = data_frame.apply(np.sum, axis = 1) # displaying DataFrame print('--------------------After------------------') print(data_frame)
Output
If you run the above program, you will get the following results.
--------------------Before------------------ Maths Programming 0 10 23 1 34 12 2 53 43 --------------------After------------------ Maths Programming Multiply 0 10 23 33 1 34 12 46 2 53 43 96
Conclusion
In the above ways, we can use apply() method of DataFrame to apply a function for all the rows. If you have any doubts regarding the tutorial, mention them in the comment section.
- Related Articles
- Apply function to every row in a Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Apply uppercase to a column in Pandas dataframe
- How to apply the aggregation list on every group of pandas DataFrame?
- Python Pandas - How to delete a row from a DataFrame
- How to append a list as a row to a Pandas DataFrame in Python?
- Deleting a DataFrame row in Python Pandas based on column value
- How to get nth row in a Pandas DataFrame?
- Python Pandas – How to use Pandas DataFrame tail( ) function
- Python - Change column names and row indexes in Pandas DataFrame
- Add a row at top in pandas DataFrame
- How to add header row to a Pandas Dataframe?
- Python Pandas - How to select rows from a DataFrame by passing row label
- How to add one row in an existing Pandas DataFrame?
- How to apply functions element-wise in a dataframe in Python?
