@@ -79,14 +79,15 @@ pandas objects can be split on any of their axes. The abstract definition of
79
79
grouping is to provide a mapping of labels to group names. To create a GroupBy
80
80
object (more on what the GroupBy object is later), you may do the following:
81
81
82
- .. code-block :: python
82
+ .. ipython :: python
83
83
:flake8- group: None
84
84
:flake8- add- ignore: F821
85
+ :verbatim:
85
86
86
87
# default is axis=0
87
- >> > grouped = obj.groupby(key)
88
- >> > grouped = obj.groupby(key, axis = 1 )
89
- >> > grouped = obj.groupby([key1, key2])
88
+ grouped = obj.groupby(key)
89
+ grouped = obj.groupby(key, axis = 1 )
90
+ grouped = obj.groupby([key1, key2])
90
91
91
92
The mapping can be specified many different ways:
92
93
@@ -141,16 +142,15 @@ but the specified columns
141
142
These will split the DataFrame on its index (rows). We could also split by the
142
143
columns:
143
144
144
- .. ipython ::
145
+ .. ipython :: python
145
146
146
- In [4]: def get_letter_type(letter):
147
- ...: if letter.lower() in 'aeiou':
148
- ...: return 'vowel'
149
- ...: else:
150
- ...: return 'consonant'
151
- ...:
147
+ def get_letter_type (letter ):
148
+ if letter.lower() in ' aeiou' :
149
+ return ' vowel'
150
+ else :
151
+ return ' consonant'
152
152
153
- In [5]: grouped = df.groupby(get_letter_type, axis=1)
153
+ grouped = df.groupby(get_letter_type, axis = 1 )
154
154
155
155
pandas :class: `~pandas.Index ` objects support duplicate values. If a
156
156
non-unique index is used as the group key in a groupby operation, all values
@@ -251,11 +251,11 @@ the length of the ``groups`` dict, so it is largely just a convenience:
251
251
gb = df.groupby(' gender' )
252
252
253
253
254
- .. ipython ::
254
+ .. ipython :: ipython
255
255
:flake8-group: None
256
256
:flake8-set-ignore: E999, E225
257
+ :verbatim:
257
258
258
- @verbatim
259
259
In [1]: gb.<TAB>
260
260
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
261
261
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
@@ -409,23 +409,21 @@ Iterating through groups
409
409
With the GroupBy object in hand, iterating through the grouped data is very
410
410
natural and functions similarly to :py:func: `itertools.groupby `:
411
411
412
- .. ipython ::
412
+ .. ipython :: python
413
413
414
- In [4]: grouped = df.groupby('A')
414
+ grouped = df.groupby(' A' )
415
415
416
- In [5]: for name, group in grouped:
417
- ...: print(name)
418
- ...: print(group)
419
- ...:
416
+ for name, group in grouped:
417
+ print (name)
418
+ print (group)
420
419
421
420
In the case of grouping by multiple keys, the group name will be a tuple:
422
421
423
- .. ipython ::
422
+ .. ipython :: python
424
423
425
- In [5]: for name, group in df.groupby(['A', 'B']):
426
- ...: print(name)
427
- ...: print(group)
428
- ...:
424
+ for name, group in df.groupby([' A' , ' B' ]):
425
+ print (name)
426
+ print (group)
429
427
430
428
See :ref: `timeseries.iterating-label `.
431
429
@@ -924,16 +922,15 @@ for both ``aggregate`` and ``transform`` in many standard use cases. However,
924
922
925
923
The dimension of the returned result can also change:
926
924
927
- .. ipython ::
925
+ .. ipython :: python
928
926
929
- In [8]: grouped = df.groupby('A')['C']
927
+ grouped = df.groupby(' A' )[' C' ]
930
928
931
- In [10]: def f(group):
932
- ....: return pd.DataFrame({'original': group,
933
- ....: 'demeaned': group - group.mean()})
934
- ....:
929
+ def f (group ):
930
+ return pd.DataFrame({' original' : group,
931
+ ' demeaned' : group - group.mean()})
935
932
936
- In [11]: grouped.apply(f)
933
+ grouped.apply(f)
937
934
938
935
``apply `` on a Series can operate on a returned value from the applied function,
939
936
that is itself a series, and possibly upcast the result to a DataFrame:
@@ -1316,9 +1313,10 @@ Now, to find prices per store/product, we can simply do:
1316
1313
Piping can also be expressive when you want to deliver a grouped object to some
1317
1314
arbitrary function, for example:
1318
1315
1319
- .. code-block :: python
1316
+ .. ipython :: python
1320
1317
:flake8- group: None
1321
1318
:flake8- add- ignore: F821
1319
+ :verbatim:
1322
1320
1323
1321
df.groupby([' Store' , ' Product' ]).pipe(report_func)
1324
1322
0 commit comments