pandas_groupby
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pandas_groupby [2024/03/26 21:55] – [extract groupby object by key] raju | pandas_groupby [2024/03/26 22:25] (current) – [groupby slicing] raju | ||
---|---|---|---|
Line 319: | Line 319: | ||
5 bar 0 6 | 5 bar 0 6 | ||
</ | </ | ||
+ | |||
+ | ==== groupby slicing ==== | ||
+ | Consider | ||
+ | < | ||
+ | In [1]: | ||
+ | import pandas as pd | ||
+ | import numpy as np | ||
+ | rand = np.random.RandomState(1) | ||
+ | df = pd.DataFrame({' | ||
+ | ' | ||
+ | ' | ||
+ | |||
+ | In [2]: | ||
+ | df | ||
+ | Out[2]: | ||
+ | | ||
+ | 0 foo 1.624345 | ||
+ | 1 bar -0.611756 | ||
+ | 2 foo -0.528172 | ||
+ | 3 bar -1.072969 | ||
+ | 4 foo 0.865408 | ||
+ | 5 bar -2.301539 | ||
+ | </ | ||
+ | |||
+ | Group by on column ' | ||
+ | < | ||
+ | In [3]: | ||
+ | gb = df.groupby([' | ||
+ | </ | ||
+ | |||
+ | You can use get_group() to get a single group | ||
+ | < | ||
+ | In [4]: | ||
+ | gb.get_group(' | ||
+ | Out[4]: | ||
+ | | ||
+ | 0 foo 1.624345 | ||
+ | 2 foo -0.528172 | ||
+ | 4 foo 0.865408 | ||
+ | </ | ||
+ | |||
+ | You can select different columns using the groupby slicing: | ||
+ | < | ||
+ | In [5]: | ||
+ | gb[[' | ||
+ | Out[5]: | ||
+ | | ||
+ | 0 foo 1.624345 | ||
+ | 2 foo -0.528172 | ||
+ | 4 foo 0.865408 | ||
+ | |||
+ | In [6]: | ||
+ | gb[[' | ||
+ | Out[6]: | ||
+ | C | ||
+ | 0 5 | ||
+ | 2 11 | ||
+ | 4 14 | ||
+ | </ | ||
+ | |||
+ | Ref: | ||
+ | * https:// | ||
==== apply a function on each group ==== | ==== apply a function on each group ==== |
pandas_groupby.1711490123.txt.gz · Last modified: 2024/03/26 21:55 by raju