Skip to content

Commit 5f34933

Browse files
DavidRosenTomAugspurger
authored andcommitted
DOC: Example for adding a calculated column in SQL and Pandas (#28182)
* Add example for adding a calculated column
1 parent 2518040 commit 5f34933

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

doc/source/getting_started/comparison/comparison_with_sql.rst

+14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ With pandas, column selection is done by passing a list of column names to your
4949
Calling the DataFrame without the list of column names would display all columns (akin to SQL's
5050
``*``).
5151

52+
In SQL, you can add a calculated column:
53+
54+
.. code-block:: sql
55+
56+
SELECT *, tip/total_bill as tip_rate
57+
FROM tips
58+
LIMIT 5;
59+
60+
With pandas, you can use the :meth:`DataFrame.assign` method of a DataFrame to append a new column:
61+
62+
.. ipython:: python
63+
64+
tips.assign(tip_rate=tips['tip'] / tips['total_bill']).head(5)
65+
5266
WHERE
5367
-----
5468
Filtering in SQL is done via a WHERE clause.

0 commit comments

Comments
 (0)