【问题标题】:List object has no attribute columns列表对象没有属性列
【发布时间】:2020-10-26 20:44:44
【问题描述】:

我正在尝试对简单数据集执行最小-最大缩放

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

下面的代码给我一个错误

AttributeError: 'list' 对象没有属性 '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

【问题讨论】:

  • 即使你称它为 df,列表也不是数据框
  • @Chris,当机立断!刚刚修好了!谢谢!

标签: python pandas scaling


【解决方案1】:

您的 min_max_scaling 函数需要一个 pandas 数据框实例,但您正在向它传递一个列表。如下更改代码应该可以工作。

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)

【讨论】:

    【解决方案2】:

    我猜你有一个数据框 df 并且对于 df 中的每一列,你想进行(最大-最小)缩放吗?

    那么你不应该将一个列表而是一个数据框传递给你的函数。

    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 = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
    df_normalized = min_max_scaling(df)
    

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      • 2022-01-19
      • 2017-06-25
      • 2016-05-14
      • 2016-12-21
      相关资源
      最近更新 更多