w3resource

Standardizing numerical data using Z-Score scaling in Pandas


8. Standardizing Numerical Data Using Z-Score Scaling

Write a Pandas program to standardize numerical data using Z-Score scaling.

This exercise shows how to standardize numerical data using Z-score scaling (StandardScaler).

Sample Solution :

Code :

import pandas as pd
from sklearn.preprocessing import StandardScaler

# Load the dataset
df = pd.read_csv('data.csv')

# Initialize the StandardScaler
scaler = StandardScaler()

# Apply Z-score scaling to the 'Age' and 'Salary' columns
df[['Age', 'Salary']] = scaler.fit_transform(df[['Age', 'Salary']])

# Output the standardized dataset
print(df)

Output:

   ID      Name       Age  Gender    Salary  Target
0   1      Sara -0.719874  Female -1.207020       0
1   2    Ophrah  0.404929    Male -0.278543       1
2   3    Torben -1.394756    Male  0.649934       0
3   4  Masaharu  1.529732    Male  1.578410       1
4   5      Kaya       NaN  Female -0.742781       0
5   6   Abaddon  0.179969    Male       NaN       1

Explanation:

  • Loaded the dataset using Pandas.
  • Initialized the StandardScaler from Scikit-learn.
  • Applied Z-score scaling (standardization) to the 'Age' and 'Salary' columns, centering them around zero.
  • Displayed the standardized dataset.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to standardize numerical columns using Z-score scaling and display the new mean and standard deviation.
  • Write a Pandas program to apply Z-score scaling and then identify any remaining outliers in the standardized data.
  • Write a Pandas program to standardize data using Z-score scaling and then compare box plots of original versus scaled data.
  • Write a Pandas program to standardize selected columns using Z-score and automatically revert the transformation.

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.