w3resource

Pandas - Standardizing a DataFrame using a custom function and applymap()


20. Standardize DataFrame Using applymap()

Write a Pandas program that uses applymap() to Standardize a DataFrame.

In this exercise, we have applied a custom function to standardize all numeric values in a DataFrame using applymap().

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({
    'A': [10, 20, 30],
    'B': [5, 15, 25]
})

# Define a custom function to standardize each element (z-score)
def standardize(x, mean, std):
    return (x - mean) / std if std != 0 else 0  # Avoid division by zero

# Apply standardization column-wise
df_standardized = df.apply(lambda col: col.apply(standardize, args=(col.mean(), col.std())))

# Output the result
print(df_standardized)

Output:

     A    B
0 -1.0 -1.0
1  0.0  0.0
2  1.0  1.0                    

Explanation:

  • Column-wise standardization: The function calculates the z-score based on the mean and standard deviation of each column, which is the correct approach for standardizing data.
  • Handling division by zero: The condition if std != 0 else 0 ensures that if a column has no variance (standard deviation is zero), the function returns 0 instead of dividing by zero.
  • Column-wise apply(): apply() is used to process each column independently and apply the standardization function element-wise.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to standardize all numeric values in a DataFrame using applymap() with a custom z-score function.
  • Write a Pandas program to apply a custom standardization function to each cell of a DataFrame using applymap() and then display the transformed DataFrame.
  • Write a Pandas program to use applymap() to normalize the entire DataFrame so that each cell represents its deviation from the mean.
  • Write a Pandas program to benchmark the performance of applymap() for standardizing a DataFrame compared to a vectorized approach.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.