VOOZH about

URL: https://www.geeksforgeeks.org/python/normalize-a-column-in-pandas/

⇱ Normalize A Column In Pandas - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Normalize A Column In Pandas

Last Updated : 11 Dec, 2020

In this article, we will learn how to normalize a column in Pandas. Let's discuss some concepts first :

  • Pandas: Pandas is an open-source library that's built on top of the NumPy library. It is a Python package that provides various data structures and operations for manipulating numerical data and statistics. It's mainly popular for importing and analyzing data much easier. Pandas is fast and it's high-performance & productive for users.
  • Data Normalization: Data Normalization could also be a typical practice in machine learning which consists of transforming numeric columns to a standard scale. In machine learning, some feature values differ from others multiple times. The features with higher values will dominate the learning process.

Steps Needed

Here, we will apply some techniques to normalize the column values and discuss these with the help of examples. For this, let's understand the steps needed for normalization with Pandas.

  1. Import Library (Pandas)
  2. Import / Load / Create data.
  3. Use the technique to normalize the column.

Examples:

Here, we create data by some random values and apply some normalization techniques on a column.

Output:

👁 Image

Dataset consists of two columns where Column 1 is not normalized but Column 2 is normalized. So we apply normalization techniques in Column 1.

Output:

👁 Image

Using The maximum absolute scaling:

The maximum absolute scaling rescales each feature between -1 and 1 by dividing every observation by its maximum absolute value. We can apply the maximum absolute scaling in Pandas using the .max() and .abs() methods, as shown below.

Output:

👁 Image

Using The min-max feature scaling:

The min-max approach (often called normalization) rescales the feature to a hard and fast range of [0,1] by subtracting the minimum value of the feature then dividing by the range. We can apply the min-max scaling in Pandas using the .min() and .max() methods.

Output :

👁 Image

Let's check with this plot.

👁 Image

Using The z-score method:

The z-score method (often called standardization) transforms the info into distribution with a mean of 0 and a typical deviation of 1. Each standardized value is computed by subtracting the mean of the corresponding feature then dividing by the quality deviation.

Output :

👁 Image

Let's check with this plot.

👁 Image

Using sklearn:

Transform features by scaling each feature to a given range. This estimator scales and translates each feature individually such that it is in the given range on the training set, e.g. between zero and one. Here, we will use minmax scaler.

Output :

👁 Image

Let's check with this plot:

Comment