So far, we've mostly looked at the whole DataFrame. But often you only care about a few specific pieces of information. Focusing on specific columns is a key analysis skill.
Engagement Message
Why might you want to look at just one column instead of the whole table?
To select a single column, you use square brackets []
with the column's name in quotes. For example, to select the 'City' column from a DataFrame named df
, you would write df['City']
.
Engagement Message
What does the use of quotes around the column name suggest about how Pandas identifies it?
The result of selecting a single column is a Pandas Series. A Series is like a one-dimensional array or a single column from your spreadsheet. It has an index and a set of values.
Engagement Message
How is a 1D Series different from a 2D DataFrame?
But what if you want to select multiple columns? For that, you use a second set of square brackets. This is because you are passing a list of column names to the selection brackets.
df[['Name', 'City']]
The inner brackets create the list of columns you want.
Engagement Message
With me so far?
When you select multiple columns, the result is a new, smaller DataFrame. This is perfect for creating a focused view for your analysis or for sharing a specific subset of your data with others.
