Skip to main content
TopMiniSite

Back to all posts

How to Apply A Function to Specific Columns In Pandas?

Published on
4 min read
How to Apply A Function to Specific Columns In Pandas? image

Best Python Pandas Guides to Buy in October 2025

1 Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter

BUY & SAVE
$43.99 $79.99
Save 45%
Python for Data Analysis: Data Wrangling with pandas, NumPy, and Jupyter
2 Pandas Cookbook: Practical recipes for scientific computing, time series, and exploratory data analysis using Python

Pandas Cookbook: Practical recipes for scientific computing, time series, and exploratory data analysis using Python

BUY & SAVE
$35.74 $49.99
Save 29%
Pandas Cookbook: Practical recipes for scientific computing, time series, and exploratory data analysis using Python
3 Effective Pandas: Patterns for Data Manipulation (Treading on Python)

Effective Pandas: Patterns for Data Manipulation (Treading on Python)

BUY & SAVE
$48.95
Effective Pandas: Patterns for Data Manipulation (Treading on Python)
4 Pandas Workout: 200 exercises to make you a stronger data analyst

Pandas Workout: 200 exercises to make you a stronger data analyst

BUY & SAVE
$49.44 $59.99
Save 18%
Pandas Workout: 200 exercises to make you a stronger data analyst
5 Hands-On Data Analysis with Pandas: A Python data science handbook for data collection, wrangling, analysis, and visualization

Hands-On Data Analysis with Pandas: A Python data science handbook for data collection, wrangling, analysis, and visualization

BUY & SAVE
$64.99
Hands-On Data Analysis with Pandas: A Python data science handbook for data collection, wrangling, analysis, and visualization
6 Effective Pandas 2: Opinionated Patterns for Data Manipulation (Treading on Python)

Effective Pandas 2: Opinionated Patterns for Data Manipulation (Treading on Python)

BUY & SAVE
$54.00
Effective Pandas 2: Opinionated Patterns for Data Manipulation (Treading on Python)
7 Pandas for Everyone: Python Data Analysis (Addison-Wesley Data & Analytics Series)

Pandas for Everyone: Python Data Analysis (Addison-Wesley Data & Analytics Series)

BUY & SAVE
$43.51 $49.99
Save 13%
Pandas for Everyone: Python Data Analysis (Addison-Wesley Data & Analytics Series)
+
ONE MORE?

To apply a function to specific columns in pandas, you can use the apply() method along with the axis parameter to specify whether you want to apply the function row-wise or column-wise. To apply a function to specific columns, you can use the apply() method along with the subset parameter to specify the columns you want to apply the function to. Additionally, you can use lambda functions to apply custom functions to specific columns in pandas. Overall, by using these techniques, you can easily apply a function to specific columns in pandas to manipulate and analyze your data efficiently.

How to apply a function to both numeric and categorical columns separately in pandas?

You can achieve this by using the .apply() method along with the .select_dtypes() method in pandas. Here is an example of how to apply a function separately to numeric and categorical columns in a DataFrame:

import pandas as pd

Sample DataFrame

data = {'A': [1, 2, 3, 4], 'B': ['foo', 'bar', 'foo', 'bar'], 'C': [5, 6, 7, 8]} df = pd.DataFrame(data)

Define a function to apply

def square(x): return x**2

Apply the function to numeric columns

numeric_cols = df.select_dtypes(include='number').columns df[numeric_cols] = df[numeric_cols].apply(square)

Apply the function to categorical columns

categorical_cols = df.select_dtypes(exclude='number').columns df[categorical_cols] = df[categorical_cols].apply(lambda x: x.str.upper())

print(df)

In this example, the square function is applied to numeric columns to square the values, and a lambda function is applied to uppercase the values in categorical columns. You can replace these functions with any custom function you want to apply to the columns.

How to apply a function to specific columns in pandas using lambda functions?

You can apply a function to specific columns in a pandas DataFrame using lambda functions by using the apply method along with the axis parameter to specify whether the function should be applied column-wise or row-wise.

Here's an example:

import pandas as pd

Create a sample DataFrame

data = { 'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8], 'C': [9, 10, 11, 12] } df = pd.DataFrame(data)

Define a lambda function to apply to specific columns

func = lambda x: x * 2 if x.name in ['A', 'B'] else x

Apply the function to specific columns using the apply method

df_updated = df.apply(func, axis=0)

print(df_updated)

In this example, the lambda function func doubles the values in columns 'A' and 'B'. By specifying axis=0, the function is applied column-wise to the DataFrame. The resulting DataFrame df_updated will have the values in columns 'A' and 'B' doubled.

How to apply a function to specific columns in pandas with parallel processing?

To apply a function to specific columns in a pandas DataFrame with parallel processing, you can use the apply method along with the multiprocessing module. Here is an example to demonstrate how to achieve this:

import pandas as pd from multiprocessing import Pool

Sample data

data = {'A': [1, 2, 3, 4], 'B': [10, 20, 30, 40], 'C': [100, 200, 300, 400]} df = pd.DataFrame(data)

Function to be applied to specific columns

def custom_function(column): return column * 2

Specify the columns to apply the function to

columns_to_process = ['B', 'C']

Create a pool of workers for parallel processing

pool = Pool()

Apply the custom function to specified columns in parallel

results = pool.map(custom_function, [df[col] for col in columns_to_process])

Update the DataFrame with the processed data

for i, col in enumerate(columns_to_process): df[col] = results[i]

Close the pool of workers

pool.close() pool.join()

print(df)

In this example, we define a custom function custom_function that multiplies the input by 2. We then specify the columns B and C that we want to apply this function to. We create a pool of workers using the Pool class from the multiprocessing module and use the map method to apply the function to the specified columns in parallel.

Finally, we update the original DataFrame with the processed data and close the pool of workers. The result will be a DataFrame with the specified columns processed in parallel.