-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Add pivot_table for DataFrame. #473
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
46367b9
feat: Add pivot_table for DataFrame.
Genesis929 da4155f
Update logic
Genesis929 dabf5ec
Update comments
Genesis929 a7fb4d0
Merge branch 'main' into pivot-table-huanc
Genesis929 c6c1be5
Remove code unused after merge.
Genesis929 5309e2d
Code update.
Genesis929 246a942
Update code example.
Genesis929 65abb6b
Merge branch 'main' into pivot-table-huanc
Genesis929 49f2888
Merge branch 'main' into pivot-table-huanc
Genesis929 c38eb8e
Merge branch 'main' into pivot-table-huanc
Genesis929 fb64116
Merge branch 'main' into pivot-table-huanc
Genesis929 4bd6816
Merge branch 'main' into pivot-table-huanc
tswast 842df30
Merge branch 'main' into pivot-table-huanc
Genesis929 49c1718
Update for Tuple type.
Genesis929 a605890
Merge branch 'main' into pivot-table-huanc
Genesis929 6057c40
Update code logic
Genesis929 777a14b
Update format
Genesis929 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4711,6 +4711,88 @@ def pivot(self, *, columns, index=None, values=None): | |
""" | ||
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
||
def pivot_table(self, values=None, index=None, columns=None, aggfunc="mean"): | ||
""" | ||
Create a spreadsheet-style pivot table as a DataFrame. | ||
|
||
The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) | ||
on the index and columns of the result DataFrame. | ||
|
||
**Examples:** | ||
|
||
>>> import bigframes.pandas as bpd | ||
>>> bpd.options.display.progress_bar = None | ||
|
||
>>> df = bpd.DataFrame({ | ||
... 'Product': ['Product A', 'Product B', 'Product A', 'Product B', 'Product A', 'Product B'], | ||
... 'Region': ['East', 'West', 'East', 'West', 'West', 'East'], | ||
... 'Sales': [100, 200, 150, 100, 200, 150], | ||
... 'Rating': [3, 5, 4, 3, 3, 5] | ||
... }) | ||
>>> df | ||
Product Region Sales Rating | ||
0 Product A East 100 3 | ||
1 Product B West 200 5 | ||
2 Product A East 150 4 | ||
3 Product B West 100 3 | ||
4 Product A West 200 3 | ||
5 Product B East 150 5 | ||
<BLANKLINE> | ||
[6 rows x 4 columns] | ||
|
||
Using `pivot_table` with default aggfunc "mean": | ||
|
||
>>> pivot_table = df.pivot_table( | ||
... values=['Sales', 'Rating'], | ||
... index='Product', | ||
... columns='Region' | ||
... ) | ||
>>> pivot_table | ||
Rating Sales | ||
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. #[nit] is the indentation alright here, also below? |
||
Region East West East West | ||
Product | ||
Product A 3.5 3.0 125.0 200.0 | ||
Product B 5.0 4.0 150.0 150.0 | ||
<BLANKLINE> | ||
[2 rows x 4 columns] | ||
|
||
Using `pivot_table` with specified aggfunc "max": | ||
|
||
>>> pivot_table = df.pivot_table( | ||
... values=['Sales', 'Rating'], | ||
... index='Product', | ||
... columns='Region', | ||
... aggfunc="max" | ||
... ) | ||
>>> pivot_table | ||
Rating Sales | ||
Region East West East West | ||
Product | ||
Product A 4 3 150 200 | ||
Product B 5 5 150 200 | ||
<BLANKLINE> | ||
[2 rows x 4 columns] | ||
|
||
Args: | ||
values (str, object or a list of the previous, optional): | ||
Column(s) to use for populating new frame's values. If not | ||
specified, all remaining columns will be used and the result will | ||
have hierarchically indexed columns. | ||
|
||
index (str or object or a list of str, optional): | ||
Column to use to make new frame's index. If not given, uses existing index. | ||
|
||
columns (str or object or a list of str): | ||
Column to use to make new frame's columns. | ||
|
||
aggfunc (str, default "mean"): | ||
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. [nit] no newline like after other params |
||
Aggregation function name to compute summary statistics (e.g., 'sum', 'mean'). | ||
|
||
Returns: | ||
DataFrame: An Excel style pivot table. | ||
""" | ||
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE) | ||
|
||
def stack(self, level=-1): | ||
""" | ||
Stack the prescribed level(s) from columns to index. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we also test for the default values of these params?