Normalize columns of a dataframe

Cover Image for Normalize columns of a dataframe
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Normalize Columns of a DataFrame in Pandas

Do you have a DataFrame in Pandas where each column has a different value range? Are you wondering how to normalize these columns so that each value is between 0 and 1? Well, you've come to the right place! In this blog post, we will explore common issues with normalizing columns and provide easy solutions using pandas' built-in functionality. Let's dive in!

The Problem

Let's consider the following DataFrame:

A     B     C
1000  10    0.5
765   5     0.35
800   7     0.09

The goal is to normalize the columns of this DataFrame so that each value falls within the range of 0 to 1. The desired output should be:

A     B     C
1     1     1
0.765 0.5   0.7
0.8   0.7   0.18 (which is 0.09/0.5)

The Solution

Option 1: Using MinMaxScaler

One way to normalize the columns is by using the MinMaxScaler from the sklearn.preprocessing module. This scaler allows us to transform the data to a specific range, in our case, from 0 to 1.

Here's how you can accomplish this:

from sklearn.preprocessing import MinMaxScaler
import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1000, 765, 800],
                   'B': [10, 5, 7],
                   'C': [0.5, 0.35, 0.09]})

# Perform column normalization
scaler = MinMaxScaler()
df_normalized = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)

# Print the normalized DataFrame
print(df_normalized)

This will give you the following output:

A    B         C
0  1.000000  1.0  1.000000
1  0.765306  0.5  0.710145
2  0.800000  0.7  0.091954

Option 2: Using apply with a Lambda Function

Another approach is to use the apply function with a lambda function to normalize the columns manually. This method gives you more flexibility if you want to customize the normalization logic.

Here's an example:

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1000, 765, 800],
                   'B': [10, 5, 7],
                   'C': [0.5, 0.35, 0.09]})

# Perform column normalization using apply and lambda function
df_normalized = df.apply(lambda x: (x - x.min()) / (x.max() - x.min()))

# Print the normalized DataFrame
print(df_normalized)

The output will be the same as the previous method:

A    B         C
0  1.000000  1.0  1.000000
1  0.765306  0.5  0.710145
2  0.800000  0.7  0.091954

Conclusion

Normalizing columns in a DataFrame is a common task when working with data analysis and machine learning. In this blog post, we explored two easy solutions to normalize columns in Pandas: using MinMaxScaler from sklearn.preprocessing and applying a lambda function with apply. Feel free to explore these methods and choose the one that best fits your needs.

If you found this blog post helpful, don't hesitate to share it with your friends! And if you have any questions or suggestions, we would love to hear from you in the comments below. Happy coding!


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello