|
| 1 | +.. _10min_tut_11_brackets_vs_parenthesis: |
| 2 | + |
| 3 | +Parentheses vs. Square Brackets in Python and pandas |
| 4 | +==================================================== |
| 5 | + |
| 6 | +Python and pandas use both **parentheses** ``()`` and **square brackets** ``[]``, but these have separate and important roles. Understanding their differences is essential for writing correct Python and pandas code. |
| 7 | + |
| 8 | +Overview of Bracket Usage |
| 9 | +------------------------- |
| 10 | + |
| 11 | ++------------------------------------+-------------------------------+------------------------------+ |
| 12 | +| Context | Parentheses ``()`` | Square Brackets ``[]`` | |
| 13 | ++====================================+===============================+==============================+ |
| 14 | +| Function and method calls | Yes: ``df.mean()`` | No | |
| 15 | ++------------------------------------+-------------------------------+------------------------------+ |
| 16 | +| Tuple creation | Yes: ``(a, b, c)`` | No | |
| 17 | ++------------------------------------+-------------------------------+------------------------------+ |
| 18 | +| List creation and item access | No | Yes: ``a[0]``, ``[1, 2]`` | |
| 19 | ++------------------------------------+-------------------------------+------------------------------+ |
| 20 | +| Dictionary item access | No | Yes: ``mydict['key']`` | |
| 21 | ++------------------------------------+-------------------------------+------------------------------+ |
| 22 | +| pandas column selection | No | Yes: ``df['A']``, | |
| 23 | +| | | ``df[['A', 'B']]`` | |
| 24 | ++------------------------------------+-------------------------------+------------------------------+ |
| 25 | +| pandas row selection and slicing | No | Yes: ``df[0:5]``, | |
| 26 | +| | | ``df.loc[2]`` | |
| 27 | ++------------------------------------+-------------------------------+------------------------------+ |
| 28 | +| Boolean indexing | No | Yes: ``df[df['col'] > 10]`` | |
| 29 | ++------------------------------------+-------------------------------+------------------------------+ |
| 30 | +| Grouping/logical expressions | Yes: ``(x + y) * z`` | No | |
| 31 | ++------------------------------------+-------------------------------+------------------------------+ |
| 32 | + |
| 33 | +Detailed Explanations and Examples |
| 34 | +---------------------------------- |
| 35 | + |
| 36 | +**Parentheses ``()``** |
| 37 | + |
| 38 | +- Used to *call* functions and methods, enclosing arguments: |
| 39 | + .. code-block:: python |
| 40 | +
|
| 41 | + df.mean() |
| 42 | + print("Hello, world!") |
| 43 | + sum([1, 2, 3]) |
| 44 | +
|
| 45 | +- Used to create *tuples*, which are immutable ordered collections: |
| 46 | + .. code-block:: python |
| 47 | +
|
| 48 | + coordinates = (10, 20) |
| 49 | + empty_tuple = () |
| 50 | +
|
| 51 | +- Used for *grouping expressions* in mathematics and logic: |
| 52 | + .. code-block:: python |
| 53 | +
|
| 54 | + result = (1 + 2) * 3 |
| 55 | + is_valid = (score > 0) and (score < 100) |
| 56 | +
|
| 57 | +- Used to spread Python statements over multiple lines: |
| 58 | + |
| 59 | + .. code-block:: python |
| 60 | +
|
| 61 | + total = ( |
| 62 | + 1 + |
| 63 | + 2 + |
| 64 | + 3 |
| 65 | + ) |
| 66 | +
|
| 67 | +**Square Brackets ``[]``** |
| 68 | + |
| 69 | +- Used to *define* and *access* elements of Python lists: |
| 70 | + .. code-block:: python |
| 71 | +
|
| 72 | + numbers = [1, 2, 3, 4] |
| 73 | + first = numbers[0] |
| 74 | + sub = numbers[1:3] |
| 75 | +
|
| 76 | +- Used to *access* values in dictionaries by key: |
| 77 | + .. code-block:: python |
| 78 | +
|
| 79 | + prices = {'apple': 40, 'banana': 10} |
| 80 | + apple_price = prices['apple'] |
| 81 | +
|
| 82 | +- Used for all kinds of *indexing* and *selection* in pandas DataFrames and Series: |
| 83 | + |
| 84 | + *Single column selection* (returns Series): |
| 85 | + .. code-block:: python |
| 86 | +
|
| 87 | + df['A'] |
| 88 | +
|
| 89 | + *Multiple columns selection* (returns DataFrame): |
| 90 | + .. code-block:: python |
| 91 | +
|
| 92 | + df[['A', 'B']] |
| 93 | +
|
| 94 | + Here, the inner brackets create a Python list of column labels, and the outer brackets are pandas selection syntax. |
| 95 | +
|
| 96 | + *Row selection and slicing*: |
| 97 | + .. code-block:: python |
| 98 | +
|
| 99 | + df[0:2] # selects rows 0 and 1 by integer position |
| 100 | + df.loc[2] # selects row with label/index 2 |
| 101 | + df.iloc[2] # selects row at integer position 2 |
| 102 | +
|
| 103 | + *Boolean indexing (row filtering)*: |
| 104 | + .. code-block:: python |
| 105 | +
|
| 106 | + df[df['A'] > 5] # returns only rows where column 'A' is greater than 5 |
| 107 | +
|
| 108 | +Key Points to Remember |
| 109 | +---------------------- |
| 110 | + |
| 111 | +- **Parentheses** are for function/method calls, tuple creation, grouping, and continuation of statements. |
| 112 | +- **Square brackets** are for creating and accessing lists, dictionary values, slicing sequences, and—critically for pandas—selecting/subsetting columns and rows. |
| 113 | +- In pandas, *single* square brackets select a single column as a Series (``df['A']``), while *double* square brackets select multiple columns as a DataFrame (``df[['A', 'B']]``) because the *inner brackets create a Python list* of column labels. |
| 114 | +- Boolean indexing in pandas always uses square brackets enclosing a boolean Series: ``df[df['A'] > 5]``. |
| 115 | + |
| 116 | +Common Pitfalls |
| 117 | +--------------- |
| 118 | + |
| 119 | +- Attempting to use parentheses for list/tensor/column access or slicing will result in errors. |
| 120 | +- Using single brackets with a list inside (like ``df[['A']]``) still returns a DataFrame, not a Series—bracket count and the type of object inside matters. |
| 121 | +- Remember that method calls (like ``df.mean()`` or ``df.groupby('A')``) always require parentheses, even if there are no arguments. |
| 122 | + |
| 123 | +Example Summary |
| 124 | +--------------- |
| 125 | + |
| 126 | +.. code-block:: python |
| 127 | +
|
| 128 | + import pandas as pd |
| 129 | + df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) |
| 130 | +
|
| 131 | + # Function and method calls |
| 132 | + df.mean() |
| 133 | +
|
| 134 | + # Tuple creation |
| 135 | + t = (1, 2, 3) |
| 136 | +
|
| 137 | + # List creation/access |
| 138 | + mylist = [10, 20, 30] |
| 139 | + first_item = mylist[0] |
| 140 | +
|
| 141 | + # pandas column selection |
| 142 | + df['A'] |
| 143 | + df[['A', 'B']] |
| 144 | +
|
| 145 | + # pandas boolean indexing |
| 146 | + df[df['B'] > 4] |
| 147 | +
|
| 148 | + # Grouping/logical expressions |
| 149 | + selected = (df['A'] > 1) & (df['B'] < 6) |
| 150 | +
|
| 151 | +Getting comfortable with the distinction between parentheses and square brackets is a major milestone for every new pandas user. This understanding leads to correct code and enhanced productivity when working in both core Python and pandas. |
0 commit comments