Skip to content

Commit

Permalink
Update functions.py
Browse files Browse the repository at this point in the history
Update select_columns functions. For discussion please see pyjanitor-devs#77
  • Loading branch information
CWen001 committed Feb 24, 2019
1 parent cabe276 commit 0a542e4
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions janitor/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import warnings
from functools import partial, reduce
from typing import Dict, Iterable, List, Union
from fnmatch import translate

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -2304,31 +2305,36 @@ def currency_column_to_numeric(


@pf.register_dataframe_method
def select_columns(df: pd.DataFrame, columns: List, invert: bool = False):
def select_columns(df: pd.DataFrame, search_cols: List,
invert: bool = False):
"""
Method-chainable selection of columns.
Optional ability to invert selection of columns available as well.
Method-chaining example:
.. code-block:: python
..code-block:: python
df = pd.DataFrame(...).select_columns(['a', 'b', 'c'], invert=True)
df = pd.DataFrame(...).select_columns(['a', 'b', 'col_*'], invert=True)
:param df: A pandas DataFrame.
:param columns: A list of columns to select.
:param search_cols: A list of column names or search strings to be used to select. Valid inputs include
1) an exact column name to look for
2) a shell-style glob string (e.g., `*_thing_*`)
:param invert: Whether or not to invert the selection.
This will result in selection ofthe complement of the columns provided.
This will result in selection of the complement of the columns provided.
:returns: A pandas DataFrame with the columns selected.
"""

full_column_list = []

if invert:

return df.drop(columns=columns)
for col in search_cols:
search_string = translate(col)
columns = [col for col in df if re.match(search_string, col)]
full_column_list.extend(columns)

else:
return df[columns]
return df.drop(columns=full_column_list) if invert else df[full_column_list]


@pf.register_dataframe_method
Expand Down

0 comments on commit 0a542e4

Please sign in to comment.