Skip to content

Pandas DataFrame Column Selection Fix

DodaTech Updated 2026-06-26 1 min read

You will learn how to select columns by range, pattern, data type, and function.

The Wrong Way

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

Output:

   A  B
0  1  3
1  2  4

The Right Way

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

Output:

   A
0  1
1  2

Step-by-Step Fix

1. Understand the data structure

Check the shape, index, and dtypes before performing operations. Use .info() and .describe() to inspect.

print(df.info())
print(df.describe())
print(df.head())

2. Perform the operation

Apply the corrected method with proper parameters as shown above.

3. Verify the result

Always verify the output matches expectations:

print(result.shape)
print(result.head())
print(result.dtypes)

Prevention Tips

  • Use df.loc[:, 'start':'end'] for column range selection
  • Use df.filter(like='pattern') for string matching
  • Use df.filter(regex='pattern') for regex matching
  • Use df.select_dtypes(include=types) for dtype filtering
  • Use df['col'] for single column, df[['a','b']] for multiple

Common Mistakes

  1. Filter returns all matching columns, not just a subset order
  2. Using regex special characters in filter(like=) which uses simple substring matching
  3. Forgetting that select_dtypes uses numpy dtypes, not Python types

These mistakes appear frequently in real-world pandas code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

From a DataFrame with columns 'temp_1', 'temp_2', 'pressure', 'wind_x', 'wind_y', select all temperature columns using filter.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between filter and loc?

filter is for pattern matching on column names; loc is for explicit label ranges.

Can I select columns by dtype?

Yes: df.select_dtypes(include=['float64', 'int64']).

How do I get a list of column names?

Use df.columns.tolist() or list(df.columns).

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro