Skip to content

Commit

Permalink
DOC: Example for adding a calculated column in SQL and Pandas (#28182)
Browse files Browse the repository at this point in the history
* Add example for adding a calculated column
  • Loading branch information
DavidRosen authored and TomAugspurger committed Aug 29, 2019
1 parent 2518040 commit 5f34933
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/source/getting_started/comparison/comparison_with_sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ With pandas, column selection is done by passing a list of column names to your
Calling the DataFrame without the list of column names would display all columns (akin to SQL's
``*``).

In SQL, you can add a calculated column:

.. code-block:: sql
SELECT *, tip/total_bill as tip_rate
FROM tips
LIMIT 5;
With pandas, you can use the :meth:`DataFrame.assign` method of a DataFrame to append a new column:

.. ipython:: python
tips.assign(tip_rate=tips['tip'] / tips['total_bill']).head(5)
WHERE
-----
Filtering in SQL is done via a WHERE clause.
Expand Down

0 comments on commit 5f34933

Please sign in to comment.