[Python] How to get labels from Pandas Dataframe

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement the following example code, but the lesson I am referencing isn't quite clear. I know I am loading in data as a CSV file, but I am not sure if the dataframe.label was supposed to return the row labels or column labels. Additionally, I cannot figure out how to replace it with working code. I have tried using iloc, .columns, .to_numpy(), .as_matrix() all to no avail, and part of my struggle is determining what exactly .label did at one point (or was supposed to do). Here is the lesson I am referencing: https://www.kaggle.com/dansbecker/deep-learning-from-scratch

    and here is the code:

    def data_prep(raw):
    out_y = keras.utils.to_categorical(raw.label, num_classes)

    num_images = raw.shape[0]
    x_as_array = raw.values[:,1:]
    x_shaped_array = x_as_array.reshape(num_images, img_rows, img_cols, 1)
    out_x = x_shaped_array / 255
    return out_x, out_y

    train_file = "../input/digit-recognizer/train.csv"
    raw_data = pd.read_csv(train_file)

    x, y = data_prep(raw_data)


    The original error I got was:


    AttributeError: 'DataFrame' object has no attribute 'label'
    ​

    Can anyone shed some light into this?

    Continue reading...

Compartilhe esta Página