plydata.helper_verbs.select_at¶
-
class
plydata.helper_verbs.
select_at
(*args, **kwargs)[source]¶ Select specific columns
- Parameters
- data
dataframe
, optional Useful when not using the
>>
operator.- names
tuple
ordict
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 isFalse
.
- function
callable()
Functions to rename the column(s).
- args
tuple
Arguments to the functions. The arguments are pass to all functions.
- kwargs
dict
Keyword arguments to the functions. The keyword arguments are passed to all functions.
- data
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