Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
The ability to apply functions across rows or columns is one of the strong points of pandas. This tutorial will show you how to apply functions to Series and DataFrames using the apply()
method and related techniques.
Start by installing pandas:
pip install pandas
import pandas as pd
data = { 'A': [1, 2, 3, 4], 'B': [10, 20, 30, 40], 'C': [5, 15, 25, 35] } df = pd.DataFrame(data) print(df)
If you have a Series and you want to apply a function to each element, you can simply use the apply()
method:
def square(x): return x * x df['A'] = df['A'].apply(square) print(df)
Sometimes, you might want to apply a function across each column as a Series. For this, again use apply()
:
def range_of_series(series): return series.max() - series.min() ranges = df.apply(range_of_series) print(ranges)
If you want to apply a function across rows, set the axis
parameter to 1
:
def row_sum(row): return row.sum() df['RowSum'] = df.apply(row_sum, axis=1) print(df)
For element-wise operations on a DataFrame, you should use the applymap()
method:
def halve(x): return x / 2 half_df = df.applymap(halve) print(half_df)
For simple operations, you can use lambda functions with apply()
and applymap()
instead of defining a separate function:
# Using lambda to square each element in column 'B' df['B'] = df['B'].apply(lambda x: x**2) print(df)
You can also pass built-in functions directly to apply()
:
# Calculate the length of numbers in column 'C' as strings df['Length'] = df['C'].apply(str).apply(len) print(df)
Pandas provides a variety of ways to apply functions to Series and DataFrames, enabling powerful and flexible data transformations. The key functions to remember are apply()
for applying functions to entire rows or columns, and applymap()
for element-wise operations on DataFrames. Using these in combination with lambda functions or predefined functions allows for a wide range of data manipulations.
Pandas apply function to series:
apply
function is used to apply a function along the axis of a Pandas Series.# Apply a function to each element of a Series result_series = series.apply(lambda x: x * 2)
Apply custom function to Pandas series:
apply
to apply it to each element of the series.def custom_function(x): return x ** 2 result_series = series.apply(custom_function)
How to use apply in Pandas for series:
apply
to transform each element of a Series using a specified function.result_series = series.apply(lambda x: x + 10)
Python Pandas apply method examples:
apply
method can be used with various functions, including built-in, custom, or lambda functions.result_series = series.apply(np.sqrt) # Apply square root function
Applying a lambda function to Pandas series:
result_series = series.apply(lambda x: x * 3)
Pandas apply function to multiple series:
apply
to apply a function to multiple series, combining their values.result_series = df.apply(lambda row: row['A'] + row['B'], axis=1)
Using applymap on Pandas series:
applymap
is used for element-wise operations on entire DataFrames. For series, use apply
.result_series = series.apply(lambda x: x.upper())
Apply function element-wise to Pandas series:
apply
function can be used for element-wise operations, transforming each element based on the applied function.result_series = series.apply(lambda x: x / 2)
Apply function to each row in Pandas series:
apply
to apply a function to each row in a Pandas DataFrame.result_series = df.apply(lambda row: row.sum(), axis=1)
Pandas apply vs map for series operations:
apply
is used for element-wise and row/column-wise operations on DataFrames and Series. map
is typically used for element-wise transformations in Series.result_series = series.map(lambda x: x * 2)