plydata.helper_verbs.select_at

class plydata.helper_verbs.select_at(*args, **kwargs)[source]

Select specific columns

Parameters
datadataframe, optional

Useful when not using the >> operator.

namestuple or dict

Names of columns in dataframe. If a tuple, they should be names of columns. If a dict, they keys must be in.

  • startswithstr or tuple, optional

    All column names that start with this string will be included.

  • endswithstr or tuple, optional

    All column names that end with this string will be included.

  • containsstr or tuple, optional

    All column names that contain with this string will be included.

  • matchesstr or regex or tuple, optional

    All column names that match the string or a compiled regex pattern will be included. A tuple can be used to match multiple regexs.

  • dropbool, optional

    If True, the selection is inverted. The unspecified/unmatched columns are returned instead. Default is False.

functioncallable()

Functions to rename the column(s).

argstuple

Arguments to the functions. The arguments are pass to all functions.

kwargsdict

Keyword arguments to the functions. The keyword arguments are passed to all functions.

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from plydata import *
>>> df = pd.DataFrame({
...     'alpha': list('aaabbb'),
...     'beta': list('babruq'),
...     'theta': list('cdecde'),
...     'x': [1, 2, 3, 4, 5, 6],
...     'y': [6, 5, 4, 3, 2, 1],
...     'z': [7, 9, 11, 8, 10, 12]
... })

Select the listed columns and rename them to upper case.

>>> df >> select_at(('alpha', 'x'), str.upper)
  ALPHA  X
0     a  1
1     a  2
2     a  3
3     b  4
4     b  5
5     b  6

Select columns that contain the string eta and rename them name to upper case.

>>> df >> select_at(dict(contains='eta'), str.upper)
  BETA THETA
0    b     c
1    a     d
2    b     e
3    r     c
4    u     d
5    q     e

Group columns are always selected.

>>> df >> group_by('beta') >> select_at(('alpha', 'x'), str.upper)
groups: ['beta']
  beta ALPHA  X
0    b     a  1
1    a     a  2
2    b     a  3
3    r     b  4
4    u     b  5
5    q     b  6