Standardizing numerical data using Z-Score scaling in Pandas
Pandas: Machine Learning Integration Exercise-8 with Solution
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.
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/pandas/pandas-standardize-numerical-data-using-z-score-scaling.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics