Skip to content

ENH: Crosstab support for setting the margins name #15972 #15973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pandas/tools/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def _convert_by(by):


def crosstab(index, columns, values=None, rownames=None, colnames=None,
aggfunc=None, margins=False, dropna=True, normalize=False):
aggfunc=None, margins=False, dropna=True, normalize=False,
margins_name='All'):
"""
Compute a simple cross-tabulation of two (or more) factors. By default
computes a frequency table of the factors unless an array of values and an
Expand Down Expand Up @@ -420,6 +421,9 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
- If margins is `True`, will also normalize margin values.

.. versionadded:: 0.18.1
margins_name : string, default 'All'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a ..versionadded:: 0.20.0 for this parameter?

Name of the row / column that will contain the totals
when margins is True.


Notes
Expand Down Expand Up @@ -488,14 +492,16 @@ def crosstab(index, columns, values=None, rownames=None, colnames=None,
df = DataFrame(data)
df['__dummy__'] = 0
table = df.pivot_table('__dummy__', index=rownames, columns=colnames,
aggfunc=len, margins=margins, dropna=dropna)
aggfunc=len, margins=margins, dropna=dropna,
margins_name=margins_name)
table = table.fillna(0).astype(np.int64)

else:
data['__dummy__'] = values
df = DataFrame(data)
table = df.pivot_table('__dummy__', index=rownames, columns=colnames,
aggfunc=aggfunc, margins=margins, dropna=dropna)
aggfunc=aggfunc, margins=margins, dropna=dropna,
margins_name=margins_name)

# Post-process
if normalize is not False:
Expand Down