Skip to content

Commit 908cae5

Browse files
committed
ENH: crosstab prototype function, API needs fleshing out, GH #170
1 parent 9bc20d6 commit 908cae5

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ pandas 0.7.0
7070
NA values inserted in non-numeric columns (GH #614)
7171
- Can pass a list of dicts or Series to ``DataFrame.append`` to concatenate
7272
multiple rows (GH #464)
73+
- Add ``level`` argument to ``DataFrame.xs`` for selecting data from other
74+
MultiIndex levels (GH #371, GH #629)
7375

7476
**API Changes**
7577

pandas/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
from pandas.util.testing import debug
3131

3232
from pandas.tools.merge import merge, concat
33-
from pandas.tools.pivot import pivot_table
33+
from pandas.tools.pivot import pivot_table, crosstab

pandas/tools/pivot.py

+28
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,31 @@ def _convert_by(by):
165165
by = list(by)
166166
return by
167167

168+
def crosstab(rows, columns):
169+
"""
170+
Compute a simple cross-tabulation of two (or more) factors
171+
172+
Parameters
173+
----------
174+
rows :
175+
columns :
176+
177+
Returns
178+
-------
179+
crosstab : DataFrame
180+
"""
181+
rname = cname = None
182+
if isinstance(rows, Series):
183+
rname = rows.name
184+
185+
if isinstance(columns, Series):
186+
cname = columns.name
187+
188+
df = DataFrame({'rows' : rows, 'columns' : columns})
189+
table = df.groupby(['rows', 'columns']).size()
190+
191+
result = table.unstack()
192+
result.columns.name = cname
193+
result.index.name = rname
194+
195+
return result

0 commit comments

Comments
 (0)