30 Mins

4. Data Selection: Basic

Accessing columns with dictionary bracket notation

In Pandas, the most straightforward way to access a single column is by using the dictionary-style bracket notation. This treats the DataFrame like a dictionary where the keys are the column names.

When you pass a string to the square brackets, Pandas returns a Series object, which represents the entire column of data along with its index. This is the standard, safe way to access data, especially when column names contain spaces or special characters.

Operator Note: Always use bracket notation if your column name contains spaces (e.g., df['Total Revenue']) because dot notation will fail in those cases.
Python 3
import pandas as pd

data = {'City': ['London', 'Paris', 'Rome'], 'Population': [8.9, 2.1, 2.8]}
df = pd.DataFrame(data)

# Accessing the City column
cities = df['City']
print(cities)

Selecting multiple columns as sub-DataFrames

To select multiple columns at once, you must pass a list of column names inside the square brackets. This is a common point of confusion for beginners: if you pass a list, you get a new DataFrame back, not a Series.

By providing a list like [ColA, ColB], you create a subset of your original data containing only those columns, which is essential for feature selection and exploratory analysis.

Operator Note: Remember: one set of brackets equals a Series, two sets of brackets [[]] equals a DataFrame.
Python 3
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})

# Selecting multiple columns
subset = df[['A', 'C']]
print(subset)

Row slicing using coordinate bounds

Row slicing allows you to extract specific ranges of rows based on their integer index positions. Using the syntax df[start:end], you can grab a segment of your dataset.

This is similar to standard Python list slicing. The start index is inclusive, while the end index is exclusive. This is highly efficient for taking quick looks at a specific block of your dataset, such as the first 5 or last 10 rows.

Operator Note: Always remember that the stop index in slicing is exclusive; if you want index 4 included, you must slice to 5.
Python 3
import pandas as pd

df = pd.DataFrame({'Val': range(10)})

# Slicing rows 2 through 4 (index 2, 3)
slice_df = df[2:4]
print(slice_df)

Scalar cell element extraction

Sometimes you need to access a single specific value at the intersection of a row and a column. While there are advanced locators like .at and .iat, the most common way to isolate a single scalar value is to combine column access with indexing.

By identifying the column first and then providing an integer index, you retrieve the scalar value located at that position. This is useful for extracting constants or performing specific validations on individual data points.

Operator Note: For production code where speed is critical, prefer using .iat[row, col] as it is optimized for single scalar extraction.
Python 3
import pandas as pd

df = pd.DataFrame({'Price': [100, 200, 300]})

# Extracting the value from the first row of 'Price'
scalar_val = df['Price'][0]
print(scalar_val)

Knowledge Check

Ready to test your understanding of 4. Data Selection: Basic?

Back