Skip to content

Commit d5b38a9

Browse files
authored
Example with and without categories with no data
Now shows correct output for crosstab(foo, bar) and show another example where categories are preserved by setting "dropna=False".
1 parent 5396f66 commit d5b38a9

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pandas/core/reshape/pivot.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,19 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
429429
430430
>>> foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
431431
>>> bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
432+
>>> crosstab(foo, bar) # 'c' and 'f' are not represented in the data,
433+
# and will not be shown in the output because
434+
# dropna is True by default. Set 'dropna=False'
435+
# to preserve categories with no data
436+
... # doctest: +SKIP
437+
col_0 d e
438+
row_0
439+
a 1 0
440+
b 0 1
441+
432442
>>> crosstab(foo, bar, dropna=False) # 'c' and 'f' are not represented
433-
# in the data, but they still will be counted in
434-
# the output
443+
# in the data, but they still will be counted
444+
# and shown in the output
435445
... # doctest: +SKIP
436446
col_0 d e f
437447
row_0

0 commit comments

Comments
 (0)