Skip to content

Commit 440dde6

Browse files
DOC: Add section clarifying parentheses vs. brackets usage
1 parent e209a35 commit 440dde6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

doc/source/getting_started/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,3 +669,5 @@ material is enlisted in the community contributed :ref:`communitytutorials`.
669669
intro_tutorials/index
670670
comparison/index
671671
tutorials
672+
parentheses_vs_brackets
673+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Parentheses vs. Brackets in pandas
2+
==================================
3+
4+
In pandas, beginners often get confused between **parentheses `()`** and **square brackets `[]`**.
5+
Understanding the difference is essential for using pandas effectively.
6+
7+
**Parentheses `()`** are used to **call functions or methods**:
8+
9+
.. code-block:: python
10+
11+
import pandas as pd
12+
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
13+
14+
# Call the head() method to see first 5 rows
15+
df.head()
16+
17+
**Square brackets `[]`** are used to **access data or select columns**:
18+
19+
.. code-block:: python
20+
21+
# Select a single column as a Series
22+
df['A']
23+
24+
# Select multiple columns as a DataFrame
25+
df[['A', 'B']]
26+
27+
**Key points:**
28+
29+
- `()` always executes something (a function/method).
30+
- `[]` always retrieves data (like indexing or slicing).
31+
- Mixing them up is a common source of errors for new pandas users.
32+
33+
**Additional examples:**
34+
35+
.. code-block:: python
36+
37+
# Using brackets to filter rows
38+
df[df['A'] > 1]
39+
40+
# Using parentheses to chain method calls
41+
df[['A', 'B']].head()

0 commit comments

Comments
 (0)