How to normalize the elements of a Pandas DataFrame in Python

Normalizing the elements of a Pandas DataFrame converts each element in a DataFrame to a value between 0 and 1.

Solution for How to normalize the elements of a Pandas DataFrame in Python : You can use pandas.DataFrame.max() to normalize a DataFrame Call pandas.DataFrame.max() to get a Series containing the maximum value of each column of pandas.DataFrame. Call pandas.Series.max() with pandas.Series as the previous result to get the maximum value of pandas.Series. Divide each element in a DataFrame by this maximum value to normalize the DataFrame.

If a DataFrame doesn’t consist of only positive values, first find its maximum value. Then call pandas.DataFrame.min() to get a Series containing the minimum value of each column of pandas.DataFrame. Call pandas.Series.min() with pandas.Series as the previous result to get the minimum value of pandas.Series. Subtract this minimum value from each element in the DataFrame and divide the result by the difference between the maximum and minimum value.

df = pd.DataFrame({"Data1": [-10, 20, 30], "Data2": [40, -50, 60]})


how-to-normalize-the-elements-of-a-pandas-dataframe-in-python