How to Select a Column in pandas DataFrame Easily
To select a column in pandas, use
df['column_name'] or df.column_name. This returns a Series with the data from that column.Syntax
You can select a column in pandas DataFrame using two main ways:
df['column_name']: Use the column name as a string inside square brackets.df.column_name: Use the column name as an attribute (only if the name is a valid Python identifier).
Both return a pandas Series representing the column data.
python
df['column_name']
df.column_nameExample
This example shows how to create a DataFrame and select a column using both methods.
python
import pandas as pd data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]} df = pd.DataFrame(data) # Select 'age' column using bracket notation age_column = df['age'] # Select 'name' column using dot notation name_column = df.name print(age_column) print(name_column)
Output
0 25
1 30
2 35
Name: age, dtype: int64
0 Alice
1 Bob
2 Charlie
Name: name, dtype: object
Common Pitfalls
Common mistakes when selecting columns include:
- Using dot notation for column names with spaces or special characters, which causes errors.
- Confusing single brackets
[]with double brackets[[]]. Single brackets return a Series, double brackets return a DataFrame. - Trying to select multiple columns with single brackets and a string, which is invalid.
python
import pandas as pd data = {'first name': ['Alice', 'Bob'], 'age': [25, 30]} df = pd.DataFrame(data) # Wrong: dot notation with space in column name # print(df.first name) # SyntaxError # Right: bracket notation print(df['first name']) # Single bracket returns Series print(type(df['age'])) # <class 'pandas.core.series.Series'> # Double brackets return DataFrame print(type(df[['age']])) # <class 'pandas.core.frame.DataFrame'>
Output
0 Alice
1 Bob
Name: first name, dtype: object
<class 'pandas.core.series.Series'>
<class 'pandas.core.frame.DataFrame'>
Quick Reference
| Method | Usage | Returns | Notes |
|---|---|---|---|
| Bracket notation | df['column_name'] | Series | Works with any column name |
| Dot notation | df.column_name | Series | Only if column name is a valid Python identifier |
| Multiple columns | df[['col1', 'col2']] | DataFrame | Use double brackets for multiple columns |
Key Takeaways
Use df['column_name'] to select any column safely.
Dot notation df.column_name works only for simple column names without spaces.
Single brackets return a Series; double brackets return a DataFrame.
Avoid dot notation for column names with spaces or special characters.
Selecting multiple columns requires double brackets with a list of names.