- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Use Pandas apply() inplace?
The apply() function in pandas is used to apply a custom function to the data frame or series. The apply() function can be used to perform transformations, computation, and other operations on the data. The apply() function returns a new Data frame or series by default. We can also modify the dataframe or series by using the inplace parameter of the apply() function. In this article, we will understand how we can use apply() function inplace with the help of examples.
Syntax of apply() Function
df.apply(func, axis=0)
Here, df is the dataframe on which we need to apply the function func. The axis parameter is used to set the applied function to the row(axis=0) or to the column(axis=1). The func can be a built-in function or a custom function.
Using apply() Method Without inplace Parameter
When we do not use the inplace parameter with apply() function, it returns a new Data Frame or series leaving the original dataframe unchanged.
Example
In the below example, we have a DataFrame with a 'Name' column. We define a custom function add_prefix() that adds the prefix 'Mr.' to each name. By using apply() on the 'Name' column, we obtain a new Series new_df with the modified names.
import pandas as pd # Create a DataFrame data = {'Name': ['John', 'Emily', 'James', 'Emma'], 'Age': [28, 32, 25, 29]} df = pd.DataFrame(data) # Function to add a prefix to names def add_prefix(name): return 'Mr. ' + name # Apply the function to the 'Name' column new_df = df['Name'].apply(add_prefix) # Output the new DataFrame print(new_df)
Output
0 Mr. John 1 Mr. Emily 2 Mr. James 3 Mr. Emma Name: Name, dtype: object
Using apply() Method inplace
By passing the inplace parameter to the apply() method we can modify the original dataframe or series in-place. We need to specify the inplace parameter as True.
Example
In the below example, we first create a DataFrame with 'Name' and 'Age' columns. Then, a custom function add_prefix() is defined to add the prefix 'Mr.' to each name. The apply() function is applied to the 'Name' column with the add_prefix() function, and the inplace=True parameter is set to modify the original DataFrame directly. Finally, the modified DataFrame is printed, showing the names in the 'Name' column with the added prefix.
import pandas as pd # Create a DataFrame data = {'Name': ['John', 'Emily', 'James', 'Emma'], 'Age': [28, 32, 25, 29]} df = pd.DataFrame(data) # Function to add a prefix to names def add_prefix(name): return 'Mr. ' + name # Apply the function to the 'Name' column in-place df['Name'].apply(add_prefix, inplace=True) # Output the modified DataFrame print(df)
Output
Name Age 0 Mr. John 28 1 Mr. Emily 32 2 Mr. James 25 3 Mr. Emma 29
Using apply Function with Multiple Columns
We can implement apply the () function to multiple columns by using the axis parameter. If we pass axis=1, the apply method is applied column-wise and if the axis parameter is passed as axis=0, the apply() method is applied row-wise.
Example
In the below example, we have a DataFrame with three columns: 'Name', 'Age', and 'Salary'. The process_data() function is defined to add the prefix 'Mr.' to the 'Name' column and double the values in the 'Salary' column. By applying this function to the specified columns using apply() with axis=1, we modify the original DataFrame df in place.
import pandas as pd # Create a DataFrame data = {'Name': ['John', 'Emily', 'James', 'Emma'], 'Age': [28, 32, 25, 29], 'Salary': [50000, 60000, 45000, 55000]} df = pd.DataFrame(data) # Function to add a prefix to names and double the salary def process_data(row): row['Name'] = 'Mr. ' + row['Name'] row['Salary'] *= 2 return row # Apply the function to multiple columns df[['Name', 'Salary']] = df[['Name', 'Salary']].apply(process_data, axis=1) # Output the modified DataFrame print(df)
Output
Name Age Salary 0 Mr. John 28 100000 1 Mr. Emily 32 120000 2 Mr. James 25 90000 3 Mr. Emma 29 110000
When to Use apply() Method inplace
The apply() method with the inplace parameter set to `True` is mostly used when we want to modify the original dataframe or series without creating a new object. This will help in saving memory and improve performance especially when working with large datasets.
Sometimes using apply() method with an inplace parameter set to True, modifying the original data can be risky as it can lead to unexpected results or loss of data if not handled carefully. It is usually recommended to take the backup of the original data before modifying the data by using apply() method.
Conclusion
In this article, we discussed how we can use the Pandas apply() method inplace and apply a custom function using it to a data frame column or series. By default the apply() method returns a new dataframe or series with the applied function. If we want to modify the original data frame we need to specify the inplace parameter as true.