Skip to content

Instantly share code, notes, and snippets.

@truevis
Last active September 18, 2025 18:07
Show Gist options
  • Select an option

  • Save truevis/b9d3719d3bbcf5087983d4fa51431e26 to your computer and use it in GitHub Desktop.

Select an option

Save truevis/b9d3719d3bbcf5087983d4fa51431e26 to your computer and use it in GitHub Desktop.
This is a demo script using pandas showing how to use apply() + lambda in different contexts: Build a sample DataFrame with two cols A & B. Filter rows that contain “4” anywhere. Square values in column A. Create a new column = A + B (row wise). Compute sum over each column.
import pandas as pd
# Create sample data for demonstration
data = {'A': [4, 8, 3, 4], 'B': [4, 5, 6, 7]}
# data = {
# 'A': [4, 8, 3, 4, 9, 2, 7, 5, 6, 1, 10, 12, 14, 4, 8, 11, 13, 15, 16, 17],
# 'B': [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
# }
df = pd.DataFrame(data)
print("Original DataFrame:\n", df.to_string(index=False))
#filter data for rows that contain 4
df_filtered = df[df.apply(lambda row: 4 in row.values, axis=1)]
print("\nDataFrame filtered for rows containing 4:\n", df_filtered.to_string(index=False))
# METHOD 1: Applying a lambda function to a Series (single column)
# The apply() function when called on a Series applies the function to each element
# lambda x: x**2 creates an anonymous function that squares each value
# This processes each value in column 'A' individually
df['A_squared'] = df['A'].apply(lambda x: x**2)
print("\nDataFrame after squaring column 'A':\n", df.to_string(index=False))
# METHOD 2: Applying a lambda function to each row of the DataFrame
# When axis=1 is specified, apply() works row-wise instead of column-wise
# lambda row: row['A'] + row['B'] creates a function that takes an entire row
# and returns the sum of values in columns 'A' and 'B' for that row
# The 'row' parameter represents a pandas Series containing all values for that row
df['Sum_AB'] = df.apply(lambda row: row['A'] + row['B'], axis=1)
print("\nDataFrame after summing columns 'A' and 'B' for each row:\n", df.to_string(index=False))
# METHOD 3: Applying a lambda function to each column of the DataFrame
# When axis=0 is specified (or omitted, as it's the default), apply() works column-wise
# lambda col: col.sum() creates a function that takes an entire column (Series)
# and returns the sum of all values in that column
# The 'col' parameter represents a pandas Series containing all values for that column
column_sums = df.apply(lambda col: col.sum(), axis=0)
print("\nSum of each column (axis=0):\n", column_sums.to_string())
# KEY DIFFERENCES:
# - df['A'].apply() works on a single column (Series) - processes each element
# - df.apply(axis=1) works on each row - processes entire rows at once
# - df.apply(axis=0) works on each column - processes entire columns at once (default behavior)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment