pandas.core.groupby.SeriesGroupBy.median#

SeriesGroupBy.median(numeric_only=False, skipna=True)[source] #

Compute median of groups, excluding missing values.

For multiple groupings, the result index will be a MultiIndex

Parameters:
numeric_onlybool, default False

Include only float, int, boolean columns.

Changed in version 2.0.0: numeric_only no longer accepts None and defaults to False.

skipnabool, default True

Exclude NA/null values. If an entire group is NA, the result will be NA.

Added in version 3.0.0.

Returns:
Series or DataFrame

Median of values within each group.

See also

Series.groupby

Apply a function groupby to a Series.

DataFrame.groupby

Apply a function groupby to each row or column of a DataFrame.

Examples

For SeriesGroupBy:

>>> lst = ["a", "a", "a", "b", "b", "b"]
>>> ser = pd.Series([7, 2, 8, 4, 3, 3], index=lst)
>>> ser
a 7
a 2
a 8
b 4
b 3
b 3
dtype: int64
>>> ser.groupby(level=0).median()
a 7.0
b 3.0
dtype: float64

For DataFrameGroupBy:

>>> data = {"a": [1, 3, 5, 7, 7, 8, 3], "b": [1, 4, 8, 4, 4, 2, 1]}
>>> df = pd.DataFrame(
...  data, index=["dog", "dog", "dog", "mouse", "mouse", "mouse", "mouse"]
... )
>>> df
 a b
 dog 1 1
 dog 3 4
 dog 5 8
mouse 7 4
mouse 7 4
mouse 8 2
mouse 3 1
>>> df.groupby(level=0).median()
 a b
dog 3.0 4.0
mouse 7.0 3.0

For Resampler:

>>> ser = pd.Series(
...  [1, 2, 3, 3, 4, 5],
...  index=pd.DatetimeIndex(
...  [
...  "2023年01月01日",
...  "2023年01月10日",
...  "2023年01月15日",
...  "2023年02月01日",
...  "2023年02月10日",
...  "2023年02月15日",
...  ]
...  ),
... )
>>> ser.resample("MS").median()
2023年01月01日 2.0
2023年02月01日 4.0
Freq: MS, dtype: float64
On this page

This Page