Skip to content

Commit 0596cb1

Browse files
jodersTomAugspurger
authored andcommitted
DOC: update the DataFrame.count docstring (#20221)
* DataFrame.count docstring reworked, examples added * review corrections * review fixes + better description * review fixes, full stop and backticks * review fix: changed summary, added quoting
1 parent 0815c43 commit 0596cb1

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

pandas/core/frame.py

+64-8
Original file line numberDiff line numberDiff line change
@@ -5851,22 +5851,78 @@ def corrwith(self, other, axis=0, drop=False):
58515851

58525852
def count(self, axis=0, level=None, numeric_only=False):
58535853
"""
5854-
Return Series with number of non-NA/null observations over requested
5855-
axis. Works with non-floating point data as well (detects NaN and None)
5854+
Count non-NA cells for each column or row.
5855+
5856+
The values `None`, `NaN`, `NaT`, and optionally `numpy.inf` (depending
5857+
on `pandas.options.mode.use_inf_as_na`) are considered NA.
58565858
58575859
Parameters
58585860
----------
58595861
axis : {0 or 'index', 1 or 'columns'}, default 0
5860-
0 or 'index' for row-wise, 1 or 'columns' for column-wise
5861-
level : int or level name, default None
5862-
If the axis is a MultiIndex (hierarchical), count along a
5863-
particular level, collapsing into a DataFrame
5862+
If 0 or 'index' counts are generated for each column.
5863+
If 1 or 'columns' counts are generated for each **row**.
5864+
level : int or str, optional
5865+
If the axis is a `MultiIndex` (hierarchical), count along a
5866+
particular `level`, collapsing into a `DataFrame`.
5867+
A `str` specifies the level name.
58645868
numeric_only : boolean, default False
5865-
Include only float, int, boolean data
5869+
Include only `float`, `int` or `boolean` data.
58665870
58675871
Returns
58685872
-------
5869-
count : Series (or DataFrame if level specified)
5873+
Series or DataFrame
5874+
For each column/row the number of non-NA/null entries.
5875+
If `level` is specified returns a `DataFrame`.
5876+
5877+
See Also
5878+
--------
5879+
Series.count: number of non-NA elements in a Series
5880+
DataFrame.shape: number of DataFrame rows and columns (including NA
5881+
elements)
5882+
DataFrame.isna: boolean same-sized DataFrame showing places of NA
5883+
elements
5884+
5885+
Examples
5886+
--------
5887+
Constructing DataFrame from a dictionary:
5888+
5889+
>>> df = pd.DataFrame({"Person":
5890+
... ["John", "Myla", None, "John", "Myla"],
5891+
... "Age": [24., np.nan, 21., 33, 26],
5892+
... "Single": [False, True, True, True, False]})
5893+
>>> df
5894+
Person Age Single
5895+
0 John 24.0 False
5896+
1 Myla NaN True
5897+
2 None 21.0 True
5898+
3 John 33.0 True
5899+
4 Myla 26.0 False
5900+
5901+
Notice the uncounted NA values:
5902+
5903+
>>> df.count()
5904+
Person 4
5905+
Age 4
5906+
Single 5
5907+
dtype: int64
5908+
5909+
Counts for each **row**:
5910+
5911+
>>> df.count(axis='columns')
5912+
0 3
5913+
1 2
5914+
2 2
5915+
3 3
5916+
4 3
5917+
dtype: int64
5918+
5919+
Counts for one level of a `MultiIndex`:
5920+
5921+
>>> df.set_index(["Person", "Single"]).count(level="Person")
5922+
Age
5923+
Person
5924+
John 2
5925+
Myla 1
58705926
"""
58715927
axis = self._get_axis_number(axis)
58725928
if level is not None:

0 commit comments

Comments
 (0)