-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC: More examples comparison with sql #12932
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
maxu777
wants to merge
9
commits into
pandas-dev:master
from
maxu777:more_examples__comparison_with_sql
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e6fea8f
Added more examples to comparison_with_sql documentation
maxu777 e796b85
Added more examples to comparison_with_sql documentation
maxu777 b42a7a2
cosmetic
maxu777 8dd8724
removed .github/CONTRIBUTING.md
maxu777 b32738b
updated according to Jeff's comments
maxu777 56d0494
cosmetic changes in code
maxu777 f64b0e6
added missing ')'
maxu777 6a4522c
fixed bugs
maxu777 e554cd7
using assign() + query() functions, changed SQL query for RANK() and …
maxu777 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,10 +372,109 @@ In pandas, you can use :meth:`~pandas.concat` in conjunction with | |
|
||
pd.concat([df1, df2]).drop_duplicates() | ||
|
||
Pandas equivalents for some SQL analytic and aggregate functions | ||
---------------------------------------------------------------- | ||
Top N rows with offset | ||
|
||
.. code-block:: sql | ||
|
||
-- MySQL | ||
SELECT * FROM tips | ||
ORDER BY tip DESC | ||
LIMIT 10 OFFSET 5; | ||
|
||
In pandas: | ||
|
||
.. ipython:: python | ||
|
||
tips.nlargest(10+5, columns='tip').tail(10) | ||
|
||
Top N rows per group | ||
|
||
.. code-block:: sql | ||
|
||
-- Oracle's ROW_NUMBER() analytic function | ||
SELECT * FROM ( | ||
SELECT | ||
t.*, | ||
ROW_NUMBER() OVER(PARTITION BY day ORDER BY total_bill DESC) AS rn | ||
FROM tips t | ||
) | ||
WHERE rn < 3 | ||
ORDER BY day, rn; | ||
|
||
Let's add a helper column: `RN` (Row Number) | ||
|
||
.. ipython:: python | ||
|
||
(tips.assign(rn=tips.sort_values(['total_bill'], ascending=False) | ||
.groupby(['day']) | ||
.cumcount() + 1) | ||
.query('rn < 3') | ||
.sort_values(['day','rn']) | ||
) | ||
|
||
the same using `rank(method='first')` function | ||
|
||
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.
|
||
.. ipython:: python | ||
|
||
(tips.assign(rnk=tips.groupby(['day'])['total_bill'] | ||
.rank(method='first', ascending=False)) | ||
.query('rnk < 3') | ||
.sort_values(['day','rnk']) | ||
) | ||
|
||
.. code-block:: sql | ||
|
||
-- Oracle's RANK() analytic function | ||
SELECT * FROM ( | ||
SELECT | ||
t.*, | ||
RANK() OVER(PARTITION BY sex ORDER BY tip) AS rnk | ||
FROM tips t | ||
WHERE tip < 2 | ||
) | ||
WHERE rnk < 3 | ||
ORDER BY sex, rnk; | ||
|
||
Let's find tips with (rank < 3) per gender group for (tips < 2). | ||
Notice that when using ``rank(method='min')`` function | ||
`rnk_min` remains the same for the same `tip` | ||
(as Oracle's RANK() function) | ||
|
||
.. ipython:: python | ||
|
||
(tips[tips['tip'] < 2] | ||
.assign(rnk_min=tips.groupby(['sex'])['tip'] | ||
.rank(method='min')) | ||
.query('rnk_min < 3') | ||
.sort_values(['sex','rnk_min']) | ||
) | ||
|
||
|
||
UPDATE | ||
------ | ||
|
||
.. code-block:: sql | ||
|
||
UPDATE tips | ||
SET tip = tip*2 | ||
WHERE tip < 2; | ||
|
||
.. ipython:: python | ||
|
||
tips.loc[tips['tip'] < 2, 'tip'] *= 2 | ||
|
||
DELETE | ||
------ | ||
|
||
.. code-block:: sql | ||
|
||
DELETE FROM tips | ||
WHERE tip > 9; | ||
|
||
In pandas we select the rows that should remain, instead of deleting them | ||
|
||
.. ipython:: python | ||
|
||
tips = tips.loc[tips['tip'] <= 9] |
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.
this file should be removed, but if you can't i will do on merge