scikit-learn-contrib/sklearn-pandas

Preserving column names when transformer requires multiple columns as input

Open

#174 opened on Oct 2, 2018

 (4 comments) (0 reactions) (0 assignees)Python (420 forks)github user discovery
good first issue

Repository metrics

Stars
 (2,850 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

I was wondering whether it is possible to preserve column names when using a transformer that requires multiple columns of the dataframe. I'll try to illustrate what I mean with an example.

from sklearn.feature_selection import SelectKBest, chi2
​
data = pd.DataFrame({
    'pet':      ['cat', 'dog', 'dog', 'fish', 'cat', 'dog', 'cat', 'fish'],
    'children': [4., 6, 3, 3, 2, 3, 5, 4],
    'salary':   [90., 24, 44, 27, 32, 59, 36, 27]})
​
mapper_fs = DataFrameMapper([(['children','salary'], SelectKBest(chi2, k=2))])
mapper_fs.fit_transform(data[['children','salary']], data['pet'])
print(mapper_fs.transformed_names_)

Which outputs ['children_salary'], whereas I would expect just ['salary']. This makes it impossible to keep track of which columns were dropped by the SelectKBest transformer. Is there currently a way to solve this problem?

Contributor guide