Skip to content

Commit f976fdd

Browse files
committed
ENH: Crosstab support for setting the margins name pandas-dev#15972
1 parent fbbcc10 commit f976fdd

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pandas/tools/pivot.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ def _convert_by(by):
386386

387387

388388
def crosstab(index, columns, values=None, rownames=None, colnames=None,
389-
aggfunc=None, margins=False, dropna=True, normalize=False):
389+
aggfunc=None, margins=False, dropna=True, normalize=False,
390+
margins_name='All'):
390391
"""
391392
Compute a simple cross-tabulation of two (or more) factors. By default
392393
computes a frequency table of the factors unless an array of values and an
@@ -420,6 +421,9 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
420421
- If margins is `True`, will also normalize margin values.
421422
422423
.. versionadded:: 0.18.1
424+
margins_name : string, default 'All'
425+
Name of the row / column that will contain the totals
426+
when margins is True.
423427
424428
425429
Notes
@@ -488,14 +492,16 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
488492
df = DataFrame(data)
489493
df['__dummy__'] = 0
490494
table = df.pivot_table('__dummy__', index=rownames, columns=colnames,
491-
aggfunc=len, margins=margins, dropna=dropna)
495+
aggfunc=len, margins=margins, dropna=dropna,
496+
margins_name=margins_name)
492497
table = table.fillna(0).astype(np.int64)
493498

494499
else:
495500
data['__dummy__'] = values
496501
df = DataFrame(data)
497502
table = df.pivot_table('__dummy__', index=rownames, columns=colnames,
498-
aggfunc=aggfunc, margins=margins, dropna=dropna)
503+
aggfunc=aggfunc, margins=margins, dropna=dropna,
504+
margins_name=margins_name)
499505

500506
# Post-process
501507
if normalize is not False:

0 commit comments

Comments
 (0)