Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Brackets in Python and pandas

A common source of confusion for those who are new to Python and pandas are the uses of different types of brackets. Hopefully this guide can clarify them for you.

Curly braces

{ and }

Curly braces are used to define Python dictionaries (dicts).

dict

Square brackets

[ and ]

Lists

Python uses square brackets to define lists.

list

They are used to retrieve an element, by passing in an index.

'B'

…or a slice:

['B', 'C']

Dictionaries

When you have a Python dict, you retrieve values by passing the key between square brackets.

2

DataFrames

Let’s create a simple Dataframe we can use for demonstration purposes. To do so, we pass a dictionary with lists of values for each column to the pd.DataFrame constructor.

Loading...

Single column

To retrieve a single column, take the DataFrame variable followed by square brackets, passing in the name (label) of a column as a string.

0 A 1 B 2 C Name: col1, dtype: object
pandas.core.series.Series

Multiple columns

If you pass multiple column names as a list, it will return a new DataFrame with just those columns in that order.

list
Loading...
pandas.core.frame.DataFrame

You’ll sometimes see that written all in one line. In this case, the inner and outer pairs of square brackets are serving different purposes.

Loading...

Boolean indexing

See Lecture 2. In this case, we’re passing in a Series of Trues and Falses between square brackets, which tells it which rows to select.

Parentheses

( and )

Parentheses serve a number of purposes in Python. Because pandas is a Python package, it uses them too.

Tuples

Python has a type called a tuple, which is like a list that can’t be modified.

tuple

Logical grouping

You can use parentheses to control the order of operations.

0.3333333333333333
1.1666666666666667

Multi-line statements

You can wrap Python statements in parentheses to split them into multiple lines. This also allows you to embed comments.

525600

can be rewritten as:

525600

Functions

In Python, parentheses are used in function definitions to specify the arguments.

Then, parentheses are used to call functions, passing in the arguments (if any).

<function __main__.add_five(num)>
11

Classes

When making a new instance of a class, you use parentheses after the class name. We saw this above with pd.DataFrame().

Angle brackets

< and >

Angle brackets are used to do comparison.

True

pandas

In pandas, that comparison can be done across all values in a Series, returning a new Series with the results.

0 6.32 1 8.10 2 4.90 Name: col3, dtype: float64
pandas.core.series.Series
0 True 1 False 2 True Name: col3, dtype: bool
pandas.core.series.Series

You’ll often see this used in boolean indexing.

Conclusion

Totally reaonable to be confused about which brackets mean what in what contexts. Be patient, it just takes time for it to sink in.

See also: How do I select a subset of a DataFrame?