apply min max scalar to multiple dataset using loop

I have n data-frames. I want to apply min- max scalar to each of them.

from sklearn import preprocessing

x = df1.values #returns a numpy array
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
#df11= pd.DataFrame(x_scaled)
x_scaled1 = pd.DataFrame(x_scaled, columns = df1.columns)

for one df. I tried several ways. Everything failed.

2 answers

  • answered 2021-05-17 06:43 Akash Dubey

    Please list out the methods you have tried for your problem. However, you can store the dataframe objects in a list. And then you can scale each of the dataframe as you loop through the list. I am doing it for two dataframes but the same applies for n number of these.

    df1 = {'col1' : np.random.randint(1,26,10) , 'col2' : np.random.randint(1,26,10)}
    df2 = {'col1' : np.random.randint(1,26,10) , 'col2' : np.random.randint(1,26,10)}
    
    df1 = pd.DataFrame(df1)
    df2 = pd.DataFrame(df2)
    
    from sklearn import preprocessing
    
    df_list = [df1,df2]
    
    def loop_scaler(df_list):
        scaled_df_list = []
        for df in df_list:
            df_array = df.values
            min_max_scaler = preprocessing.MinMaxScaler()
            df_scaled = min_max_scaler.fit_transform(df_array)
            df_scaled = pd.DataFrame(df_scaled, columns = df.columns)
            scaled_df_list.append(df_scaled)
        return scaled_df_list
    
    

    You can print and look at the scaled output:

    scaled_df_list = loop_scaler(df_list)
    for scaled_df in scaled_df_list:
        print(scaled_df.head(5))
    

    or

    scaled_df1, scaled_df2 = loop_scaler(df_list)
    print(scaled_df1.show())
    print(scaled_df2.show())
    

  • answered 2021-05-17 07:50 Mohan

    import pandas as pd
    from sklearn.preprocessing import MinMaxScaler
    data = [[-1, 2], [-0.5, 6], [0, 10], [1, 18]]
    new_data = pd.DataFrame(data)
    scaler = MinMaxScaler()
    transformed_data = pd.DataFrame(scaler.fit_transform(new_data))  
    

    This may help to solve your problem.