question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consistent ColumnTransformer for intersecting lists of columns

See original GitHub issue

I want to use sklearn.compose.ColumnTransformer consistently (not parallel, so, the second transformer should be executed only after the first) for intersecting lists of columns in this way:

log_transformer = p.FunctionTransformer(lambda x: np.log(x))
df = pd.DataFrame({'a': [1,2, np.NaN, 4], 'b': [1,np.NaN, 3, 4], 'c': [1 ,2, 3, 4]})
compose.ColumnTransformer(n_jobs=1,
                         transformers=[
                             ('num', impute.SimpleImputer() , ['a', 'b']),
                             ('log', log_transformer, ['b', 'c']),
                             ('scale', p.StandardScaler(), ['a', 'b', 'c'])
                         ]).fit_transform(df)

So, I want to use SimpleImputer for 'a', 'b', then log for 'b', 'c', and then StandardScaler for 'a', 'b', 'c'.

But:

  1. I get array of (4, 7) shape.
  2. I still get Nan in a and b columns.

So, how can I use ColumnTransformer for different columns in the manner of Pipeline?

P.S. please note that I already asked a question on SO.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
konstantin-doncovcommented, Jun 8, 2020

@justmarkham thanks, it works!

1reaction
justmarkhamcommented, Jun 8, 2020

@don-prog In ct2, specify the cols_3 columns by integer position, not by name.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Consistent ColumnTransformer for intersecting lists of columns
I want to use sklearn.compose.ColumnTransformer consistently (not parallel, so, the second transformer should ...
Read more >
Using ColumnTransformer to combine data processing steps
ColumnTransformers come in handy when you are creating a data pipeline where different columns need different transformations.
Read more >
sklearn.compose.ColumnTransformer
Applies transformers to columns of an array or pandas DataFrame. This estimator allows different columns or column subsets of the input to be...
Read more >
Screening hidden columns of pd.describe() lists-Pandas,Python
... Consistent ColumnTransformer for intersecting lists of columns · Run a function for each element in two lists in Pandas Dataframe Columns ...
Read more >
Extracting, transforming and selecting features - Apache Spark
VectorAssembler is a transformer that combines a given list of columns into a single vector column. It is useful for combining raw features...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found