pynapple.TsdFrame.groupby#
- TsdFrame.groupby(by, get_group=None)[source]#
Group pynapple object by metadata name(s).
- Parameters:
by (str or list of str) – Metadata name(s) to group by.
get_group (dictionary key, optional) – Name of the group to return.
- Returns:
Dictionary of object indices (dictionary values) corresponding to each group (dictionary keys), or pynapple object corresponding to ‘get_group’ if it has been supplied.
- Return type:
dict or pynapple object
- Raises:
ValueError – If metadata name does not exist.
Examples
>>> import pynapple as nap >>> import numpy as np >>> metadata = {"l1": [1, 2, 2], "l2": ["x", "x", "y"]} >>> tsdframe = nap.TsdFrame(t=np.arange(5), d=np.ones((5, 3)), metadata=metadata) >>> print(tsdframe) Time (s) 0 1 2 ---------- -------- -------- -------- 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 1.0 1.0 1.0 3.0 1.0 1.0 1.0 4.0 1.0 1.0 1.0 Metadata -------- -------- -------- -------- l1 1 2 2 l2 x x y dtype: float64, shape: (5, 3)
Grouping by a single row:
>>> tsdframe.groupby("l2") {'x': [0, 1], 'y': [2]}
Grouping by multiple rows:
>>> tsdframe.groupby(["l1","l2"]) {(1, 'x'): [0], (2, 'x'): [1], (2, 'y'): [2]}
Filtering to a specific group using the output dictionary:
>>> groups = tsdframe.groupby("l2") >>> tsdframe[:,groups["x"]] Time (s) 0 1 ---------- -------- -------- 0.0 1.0 1.0 1.0 1.0 1.0 2.0 1.0 1.0 3.0 1.0 1.0 4.0 1.0 1.0 Metadata -------- -------- -------- l1 1 2 l2 x x dtype: float64, shape: (5, 2)
Filtering to a specific group using the get_group argument:
>>> tsdframe.groupby("l2", get_group="x") Time (s) 0 1 ---------- -------- -------- 0.0 1.0 1.0 1.0 1.0 1.0 2.0 1.0 1.0 3.0 1.0 1.0 4.0 1.0 1.0 Metadata -------- -------- -------- l1 1 2 l2 x x dtype: float64, shape: (5, 2)