@@ -66,12 +66,21 @@ pandas objects can be split on any of their axes. The abstract definition of
66
66
grouping is to provide a mapping of labels to group names. To create a GroupBy
67
67
object (more on what the GroupBy object is later), you may do the following:
68
68
69
- .. code-block :: python
69
+ .. ipython :: python
70
+
71
+ df = pd.DataFrame([(' bird' , ' Falconiformes' , 389.0 ),
72
+ (' bird' , ' Psittaciformes' , 24.0 ),
73
+ (' mammal' , ' Carnivora' , 80.2 ),
74
+ (' mammal' , ' Primates' , np.nan),
75
+ (' mammal' , ' Carnivora' , 58 )],
76
+ index = [' falcon' , ' parrot' , ' lion' , ' monkey' , ' leopard' ],
77
+ columns = (' class' , ' order' , ' max_speed' ))
78
+ df
70
79
71
- # default is axis=0
72
- >> > grouped = obj .groupby(key )
73
- >> > grouped = obj .groupby(key , axis = 1 )
74
- >> > grouped = obj .groupby([key1, key2 ])
80
+ # default is axis=0
81
+ grouped = df .groupby(' class ' )
82
+ grouped = df .groupby(' order ' , axis = ' columns ' )
83
+ grouped = df .groupby([' class ' , ' order ' ])
75
84
76
85
The mapping can be specified many different ways:
77
86
@@ -239,7 +248,7 @@ the length of the ``groups`` dict, so it is largely just a convenience:
239
248
.. ipython ::
240
249
241
250
@verbatim
242
- In [1]: gb.<TAB>
251
+ In [1]: gb.<TAB> # noqa: E225, E999
243
252
gb.agg gb.boxplot gb.cummin gb.describe gb.filter gb.get_group gb.height gb.last gb.median gb.ngroups gb.plot gb.rank gb.std gb.transform
244
253
gb.aggregate gb.count gb.cumprod gb.dtype gb.first gb.groups gb.hist gb.max gb.min gb.nth gb.prod gb.resample gb.sum gb.var
245
254
gb.apply gb.cummax gb.cumsum gb.fillna gb.gender gb.head gb.indices gb.mean gb.name gb.ohlc gb.quantile gb.size gb.tail gb.weight
@@ -1300,12 +1309,17 @@ Now, to find prices per store/product, we can simply do:
1300
1309
Piping can also be expressive when you want to deliver a grouped object to some
1301
1310
arbitrary function, for example:
1302
1311
1303
- .. code-block :: python
1312
+ .. ipython :: python
1313
+
1314
+ def mean (groupby ):
1315
+ return groupby.mean()
1304
1316
1305
- df.groupby([' Store' , ' Product' ]).pipe(report_func )
1317
+ df.groupby([' Store' , ' Product' ]).pipe(mean )
1306
1318
1307
- where ``report_func `` takes a GroupBy object and creates a report
1308
- from that.
1319
+ where ``mean `` takes a GroupBy object and finds the mean of the Revenue and Quantity
1320
+ columns repectively for each Store-Product combination. The ``mean `` function can
1321
+ be any function that takes in a GroupBy object; the ``.pipe `` will pass the GroupBy
1322
+ object as a parameter into the function you specify.
1309
1323
1310
1324
Examples
1311
1325
--------
0 commit comments