[Python] how to apply preprocessing methods on several columns at one time in sklearn

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    My question is I have so many columns in my pandas data frame and I am trying to apply the sklearn preprocessing using dataframe mapper from sklearn-pandas library such as

    mapper= DataFrameMapper([
    ('gender',sklearn.preprocessing.LabelBinarizer()),
    ('gradelevel',sklearn.preprocessing.LabelEncoder()),
    ('subject',sklearn.preprocessing.LabelEncoder()),
    ('districtid',sklearn.preprocessing.LabelEncoder()),
    ('sbmRate',sklearn.preprocessing.StandardScaler()),
    ('pRate',sklearn.preprocessing.StandardScaler()),
    ('assn1',sklearn.preprocessing.StandardScaler()),
    ('assn2',sklearn.preprocessing.StandardScaler()),
    ('assn3',sklearn.preprocessing.StandardScaler()),
    ('assn4',sklearn.preprocessing.StandardScaler()),
    ('assn5',sklearn.preprocessing.StandardScaler()),
    ('attd1',sklearn.preprocessing.StandardScaler()),
    ('attd2',sklearn.preprocessing.StandardScaler()),
    ('attd3',sklearn.preprocessing.StandardScaler()),
    ('attd4',sklearn.preprocessing.StandardScaler()),
    ('attd5',sklearn.preprocessing.StandardScaler()),
    ('sbm1',sklearn.preprocessing.StandardScaler()),
    ('sbm2',sklearn.preprocessing.StandardScaler()),
    ('sbm3',sklearn.preprocessing.StandardScaler()),
    ('sbm4',sklearn.preprocessing.StandardScaler()),
    ('sbm5',sklearn.preprocessing.StandardScaler())
    ])


    I am just wondering whether there is another more succinct way for me to preprocess many variables at one time without writing them out explicitly.

    Another thing that I found a little bit annoying is when I transformed all the pandas data frame into arrays which sklearn can work with, they will lose the column name features, which makes the selection very difficult. Does anyone knows how to preserve the column names as the key when change the pandas data frames to np arrays?

    Thank you so much!

    Continue reading...

Compartilhe esta Página