Skip to content

Commit 28a7451

Browse files
committed
Added messages for each releveant snippet
1 parent ebc60f2 commit 28a7451

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

pandas/core/series.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,14 +1815,28 @@ def _set_name(
18151815
Parrot 30.0
18161816
Parrot 20.0
18171817
Name: Max Speed, dtype: float64
1818+
1819+
We can pass a list of values (Here: ["a", "b", "a", "b"]) to group the Series data by custom labels:
18181820
>>> ser.groupby(["a", "b", "a", "b"]).mean()
18191821
a 210.0
18201822
b 185.0
18211823
Name: Max Speed, dtype: float64
1824+
1825+
Grouping by numeric labels yields similar results: (Here: [0, 1, 0, 1]):
1826+
>>> ser.groupby([0, 1, 0, 1]).mean()
1827+
0 210.0
1828+
1 185.0
1829+
Name: Max Speed, dtype: float64
1830+
1831+
1832+
We can group by a level of the index:
18221833
>>> ser.groupby(level=0).mean()
18231834
Falcon 370.0
18241835
Parrot 25.0
18251836
Name: Max Speed, dtype: float64
1837+
1838+
1839+
We can group by a condition applied to the Series values:
18261840
>>> ser.groupby(ser > 100).mean()
18271841
Max Speed
18281842
False 25.0
@@ -1845,11 +1859,15 @@ def _set_name(
18451859
Parrot Captive 30.0
18461860
Wild 20.0
18471861
Name: Max Speed, dtype: float64
1862+
18481863
>>> ser.groupby(level=0).mean()
18491864
Animal
18501865
Falcon 370.0
18511866
Parrot 25.0
18521867
Name: Max Speed, dtype: float64
1868+
1869+
We can also group by the 'Type' level of the hierarchical index to get the mean speed for each type:
1870+
18531871
>>> ser.groupby(level="Type").mean()
18541872
Type
18551873
Captive 210.0
@@ -1865,12 +1883,14 @@ def _set_name(
18651883
b 3
18661884
dtype: int64
18671885
1886+
To include `NA` values in the group keys, set `dropna=False`:
18681887
>>> ser.groupby(level=0, dropna=False).sum()
18691888
a 3
18701889
b 3
18711890
NaN 3
18721891
dtype: int64
18731892
1893+
We can also group by a custom list (Here: ["a", "b", "a", np.nan]) with NaN values to handle missing group labels:
18741894
>>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
18751895
>>> ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
18761896
>>> ser.groupby(["a", "b", "a", np.nan]).mean()

0 commit comments

Comments
 (0)