TransWikia.com

Applying a robust scaler to column 0 to 6 of my data frame?

Data Science Asked by Ifeakachukwu okwuazu on July 17, 2021

I have a dataframe X and would like to scale columns 0 to 6 using robust scaler, how do i achieve this please? the following code only returns the scaled section of the data frame without the remaining columns, but i want the whole columns returned with 0-6 scaled only.

from sklearn.preprocessing import RobustScaler

scaler=RobustScaler()
X=pd.DataFrame(scaler.fit_transform(X),columns([['Administrative',
'Informational','ProductRelated','BounceRates','PageValues','SpecialDay']]))

X

One Answer

Check out the documentation for sklearn's columnTransformer. This allows you to apply transformations to specific column indices in your dataframe.

Note the 'passthrough' option for the transformer parameter - this will be needed for the columns that you do not wish to scale/modify.

Example taken from the documentation:

>>> import numpy as np
>>> from sklearn.compose import ColumnTransformer
>>> from sklearn.preprocessing import Normalizer
>>> ct = ColumnTransformer(
...     [("norm1", Normalizer(norm='l1'), [0, 1]),
...      ("norm2", Normalizer(norm='l1'), slice(2, 4))])
>>> X = np.array([[0., 1., 2., 2.],
...               [1., 1., 0., 1.]])
>>> # Normalizer scales each row of X to unit norm. A separate scaling
>>> # is applied for the two first and two last elements of each
>>> # row independently.
>>> ct.fit_transform(X)
array([[0. , 1. , 0.5, 0.5],
       [0.5, 0.5, 0. , 1. ]])

Answered by DataScienceRick on July 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP