Search code examples
pythonpandasscaling

List object has no attribute columns


I am trying to perform min-max scaling on the simple dataset

data2 = [10, 20, 35, 70, 100]

The following code is giving me an error

AttributeError: 'list' object has no attribute 'columns'

def min_max_scaling(df):
df_norm = df.copy()
for col in df_norm.columns:
    df_norm[col] = (df_norm[col] - df_norm[col].min()) / (df_norm[col].max() - df_norm[col].min())
return df_norm

df_normalized = min_max_scaling(data3)

df_normalized

Solution

  • Your min_max_scaling function is expecting a pandas dataframe instance but you are passing it a List. Changing the code as follows should work.

    import pandas as pd
    
    def min_max_scaling(df):
        df_norm = df.copy()
        for col in df_norm.columns:
            df_norm[col] = (df_norm[col] - df_norm[col].min()) / (df_norm[col].max() - df_norm[col].min())
        
        return df_norm
    
    data2 = [10, 20, 35, 70, 100]
    
    data2 = pd.DataFrame(data2)
    
    df_normalized = min_max_scaling(data2)
    
    print(df_normalized)