@@ -1815,27 +1815,14 @@ def _set_name(
1815
1815
Parrot 30.0
1816
1816
Parrot 20.0
1817
1817
Name: Max Speed, dtype: float64
1818
-
1819
- We can pass a list of values to
1820
- group the Series data by custom labels (Here: ["a", "b", "a", "b"]):
1821
1818
>>> ser.groupby(["a", "b", "a", "b"]).mean()
1822
1819
a 210.0
1823
1820
b 185.0
1824
1821
Name: Max Speed, dtype: float64
1825
-
1826
- Grouping by numeric labels yields similar results (Here: [0, 1, 0, 1]):
1827
- >>> ser.groupby([0, 1, 0, 1]).mean()
1828
- 0 210.0
1829
- 1 185.0
1830
- Name: Max Speed, dtype: float64
1831
-
1832
- We can group by a level of the index:
1833
1822
>>> ser.groupby(level=0).mean()
1834
1823
Falcon 370.0
1835
1824
Parrot 25.0
1836
1825
Name: Max Speed, dtype: float64
1837
-
1838
- We can group by a condition applied to the Series values:
1839
1826
>>> ser.groupby(ser > 100).mean()
1840
1827
Max Speed
1841
1828
False 25.0
@@ -1858,16 +1845,11 @@ def _set_name(
1858
1845
Parrot Captive 30.0
1859
1846
Wild 20.0
1860
1847
Name: Max Speed, dtype: float64
1861
-
1862
1848
>>> ser.groupby(level=0).mean()
1863
1849
Animal
1864
1850
Falcon 370.0
1865
1851
Parrot 25.0
1866
1852
Name: Max Speed, dtype: float64
1867
-
1868
- We can also group by the 'Type' level of the hierarchical index
1869
- to get the mean speed for each type:
1870
-
1871
1853
>>> ser.groupby(level="Type").mean()
1872
1854
Type
1873
1855
Captive 210.0
@@ -1883,16 +1865,12 @@ def _set_name(
1883
1865
b 3
1884
1866
dtype: int64
1885
1867
1886
- To include `NA` values in the group keys, set `dropna=False`:
1887
1868
>>> ser.groupby(level=0, dropna=False).sum()
1888
1869
a 3
1889
1870
b 3
1890
1871
NaN 3
1891
1872
dtype: int64
1892
1873
1893
- We can also group by a custom list with NaN values to handle
1894
- missing group labels (Here: ["a", "b", "a", np.nan]):
1895
-
1896
1874
>>> arrays = ['Falcon', 'Falcon', 'Parrot', 'Parrot']
1897
1875
>>> ser = pd.Series([390., 350., 30., 20.], index=arrays, name="Max Speed")
1898
1876
>>> ser.groupby(["a", "b", "a", np.nan]).mean()
0 commit comments