-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Implement DataFrame.value_counts #27350
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
Changes from 9 commits
3d81526
699ac42
2e06d15
9e725c7
6437cfc
078d421
619f86c
ab897a9
ef8d565
362641a
181b577
3740bc2
19a6a06
c927f4a
11521c0
8539556
af3c6f3
e1f17ac
999ca18
cd7a8b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8394,6 +8394,52 @@ def isin(self, values): | |
self.columns, | ||
) | ||
|
||
def value_counts(self): | ||
""" | ||
The number of times each unique row appears in the DataFrame. | ||
|
||
Rows that contain any NaN value are omitted from the results. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. show a versionadded tag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What version should I enter for the tag? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.0.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
Returns | ||
------- | ||
counts : Series | ||
|
||
TomAugspurger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
See Also | ||
-------- | ||
Series.value_counts: Equivalent method on Series. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have more options on the Series.value_counts, dropna for example these need to be implemented There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no option in group_by to not drop rows containing a NaN. How do I go about implementing that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be OK with raising a NotImplementedError for that case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. This changed the method pretty significantly. PTAL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The single-column case now works, but the code raises |
||
|
||
Examples | ||
-------- | ||
|
||
>>> df = pd.DataFrame({'num_legs': [2, 4, 4], 'num_wings': [2, 0, 0]}, | ||
... index=['falcon', 'dog', 'cat']) | ||
>>> df | ||
num_legs num_wings | ||
falcon 2 2 | ||
dog 4 0 | ||
cat 4 0 | ||
|
||
>>> df.value_counts() | ||
num_legs num_wings | ||
2 2 1 | ||
4 0 2 | ||
dtype: int64 | ||
|
||
>>> df1col = df[['num_legs']] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the 2nd example is showing how this works for a Series? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
>>> df1col | ||
num_legs | ||
falcon 2 | ||
dog 4 | ||
cat 4 | ||
|
||
>>> df1col.value_counts() | ||
num_legs | ||
2 1 | ||
4 2 | ||
dtype: int64 | ||
""" | ||
return self.groupby(self.columns.tolist()).size() | ||
|
||
# ---------------------------------------------------------------------- | ||
# Add plotting methods to DataFrame | ||
plot = CachedAccessor("plot", pandas.plotting.PlotAccessor) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2766,3 +2766,32 @@ def test_multiindex_column_lookup(self): | |
result = df.nlargest(3, ("x", "b")) | ||
expected = df.iloc[[3, 2, 1]] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. go ahead and make a new file, test_value_counts.py and put in pandas/tests/frame/analytics/test_value_counts.py (we will split / move analytics later) |
||
def test_data_frame_value_counts(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you split up this test? Roughly one test per "thing" you're testing (single column, raising for unsupported keyword, etc.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
# Multi column data frame. | ||
df = pd.DataFrame( | ||
{"num_legs": [2, 4, 4], "num_wings": [2, 0, 0]}, | ||
index=["falcon", "dog", "cat"], | ||
) | ||
actual = df.value_counts() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use result rather than actual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
expected = pd.Series( | ||
data=[1, 2], | ||
index=pd.MultiIndex.from_arrays( | ||
[(2, 4), (2, 0)], names=["num_legs", "num_wings"] | ||
), | ||
) | ||
tm.assert_series_equal(actual, expected) | ||
|
||
# Single column data frame. | ||
df_single_col = df[["num_legs"]] | ||
actual = df_single_col.value_counts() | ||
expected = pd.Series( | ||
data=[1, 2], index=pd.Int64Index(data=[2, 4], name="num_legs") | ||
) | ||
tm.assert_series_equal(actual, expected) | ||
|
||
# Empty data frame. | ||
df_no_cols = pd.DataFrame() | ||
actual = df_no_cols.value_counts() | ||
expected = pd.Series([], dtype=np.int64) | ||
tm.assert_series_equal(actual, expected) |
Uh oh!
There was an error while loading. Please reload this page.