Selecting Columns
Let me show you how to grab specific columns from a DataFrame. This is fundamental — you don't always need every column, just the ones that matter.
Single Column with Bracket Notation
The most common way to select a column:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
print(df['Name'])
This returns a Series — basically a single column. Think of it like extracting one column from a spreadsheet.
Multiple Columns
Need more than one column? Use double brackets:
print(df[['Name', 'City']])
Double brackets return a DataFrame instead of a Series. This matters when you want to keep working with a table structure.
Dot Notation
Here is the thing — you can also use dot notation:
print(df.Name)
It works, but I don't recommend it. If your column name has spaces or special characters, it breaks. Stick with bracket notation to be safe. I definitely did when I started, and it saved me from many headaches.
Try it Yourself →Key Takeaways
- `df['column']` selects a single column (returns Series)
- `df[['col1', 'col2']]` selects multiple columns (returns DataFrame)
- Dot notation works but is risky with special column names
- Bracket notation is the safest and most common approach