macpie.pandas.get_cols_by_prefixes#

macpie.pandas.get_cols_by_prefixes(df: DataFrame, prefixes, one_match_only=True)#

Get columns that start with the prefixes.

Parameters:
dfDataFrame
prefixes: str, or list of strs

Column labels that start with the prefixes will be returned.

one_match_only: bool, default True

If True, raise error if a prefix matches more than one column.

Returns:
dictionary

A dict that maps each prefix to list of columns (Series) that start with that prefix.

Raises:
KeyError

If one_match_only is True, yet multiple columns found.

Examples

>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
>>> df
    col1  col2
0     1     3
1     2     4
>>> df.mac.get_cols_by_prefixes("col1")
defaultdict(
    <class 'list'>,
    {
        'col1': [0    1
                 1    2
                 Name: col1, dtype: int64]
    }
)
>>> df.mac.get_cols_by_prefixes(["col1","col2"])
defaultdict(
    <class 'list'>,
    {
        'col2': [0    3
                 1    4
                 Name: col2, dtype: int64],
        'col1': [0    1
                 1    2
                 Name: col1, dtype: int64]
    }
)

If you want to allow more than one match for prefixes, set one_match_only`=`False.

>>> df.mac.get_cols_by_prefixes("col",one_match_only=False)
defaultdict(
    <class 'list'>,
    {
        'col': [0    1
                1    2
                Name: col1, dtype: int64,
                0    3
                1    4
                Name: col2, dtype: int64]
    }
)