Knowledge base / Python
pandas filter: select column and index labels
I compare name selection with a value condition on one frame, including the cond mask added in pandas 3.1.

With DataFrame.filter, distinguish label selection from value conditions. Older examples using items, like and regex concern column names or the row index. Since pandas 3.1.0, the method also accepts a cond mask. I start by identifying the version and the rule: a valid call can still return a different subset from the one a report needs.
This short reference covers label selection and the version boundary in pandas 3.1.1. The synthetic example has unique column names and index labels, and the input remains unchanged between snippets. It also replaces the damaged code blocks from the old English entry with complete calls and explicit expected selections.
One input for all the examples
import pandas as pd
sales = pd.DataFrame({
"region": ["A", "B"],
"sales_pl": [100, 200],
"sales_en": [120, 240],
"cost_pl": [60, 90],
}, index=["row_a", "row_b"])
The two observations deliberately contain different amounts. Selecting a column retains both rows’ contents. A reader might need the Polish fields or the complete sales family. Those requirements lead to different name patterns even if they initially sound similar. Decide which requirement you actually have before choosing a convenient substring.
The DataFrame.filter reference describes items for labels, like for a name substring and regex for a regular expression. Use one selection rule per call. axis="columns" selects columns and axis="index" selects rows. A DataFrame defaults to columns.
Exact names and name fragments
selected = sales.filter(items=["region", "sales_pl"], axis="columns")
| Index | region | sales_pl |
|---|---|---|
| row_a | A | 100 |
| row_b | B | 200 |
The result contains the two requested columns. A nonexistent label in items is not created as an empty column. For a required report schema, I would check completeness separately. When a missing field should be an error, an explicit column list through loc may express the requirement more clearly: all those labels must exist.
selected = sales.filter(like="sales", axis="columns")
This retains sales_pl and sales_en: 100 and 120 in the first row, then 200 and 240 in the second. The substring refers to labels. The value A in region does not participate in the match. I would rely on this selection only when the naming convention actually represents the fields the reader needs.
Prefixes and suffixes require different patterns
sales_fields = sales.filter(regex=r"^sales_", axis="columns")
polish_fields = sales.filter(regex=r"_pl$", axis="columns")
The first pattern selects sales_pl and sales_en, whose names start with sales_. The second selects sales_pl and cost_pl, whose names end with _pl. The final dollar sign belongs inside the complete regular expression. It does not replace a closing quote or parenthesis. The previous English example was truncated at precisely that point.
Decide whether the pattern should match a beginning, an ending or any substring. A new field named old_sales_pl matches the _pl suffix but not the sales_ prefix. A source schema change can therefore alter the selection without a code change. Document the rule and review selected fields after source updates. An overly broad pattern can quietly add a measure the reader never requested.

Select rows through index labels
selected = sales.filter(items=["row_b"], axis="index")
The entire second row remains: B, sales of 200 and 240, and cost of 90. This selects the label row_b, rather than a numerical position. Positional selection is a different task. Also avoid applying a DataFrame’s default axis to every pandas object: a Series only has its index axis. I write the axis explicitly here so the intended selection remains visible after later edits.
Value conditions: loc or cond since version 3.1
For Polish sales above 150, use a boolean mask:
selected = sales.loc[sales["sales_pl"] > 150, ["region", "sales_pl"]]
This retains row_b, containing B and 200. The first row fails the condition because 100 is not greater than 150. The loc reference supports this kind of selection. You can express a content condition and a column selection together while keeping the two decisions clear.
Since pandas 3.1.0, you can also pass a mask to filter:
selected = sales.filter(cond=sales["sales_pl"] > 150)
This retains all of row_b, including all four columns. That differs from the loc example, which explicitly selects two fields. Older versions do not accept cond. The current documentation also announces future deprecation of label selection through items, like and regex. I show those forms to explain existing code. Before extending a workflow, check your version’s API and consider explicit selection through loc.
filter does not rename fields either. Use the separate rename method for that task. A selected sales_pl column keeps that name until an explicit rename. I also remove the old broad memory-saving promise: a method name is not a measurement of your workflow’s cost. I would justify the choice through the intended subset and the clarity of its rule.
Exercise: add one column
Add sales_plan to the schema with any two amounts. Before executing anything, identify which existing rules will include it. Both like="sales" and the ^sales_ prefix select the new field. The _pl$ suffix and the exact list containing region and sales_pl remain unchanged. This illustrates the difference between dynamically selecting a family of names and maintaining a fixed reporting contract.
Finish by writing the reader’s requirement in one sentence, then choose an operation. For labels, identify the rule and API version. For value conditions, use loc or, since pandas 3.1, filter(cond=...). To add a group statistic beside existing observations, see pandas transform. Similar operation names do not establish that they answer the same analytical question.
Key takeaways
Does DataFrame.filter select rows by their amounts?
Since pandas 3.1.0 it accepts a cond mask. Older items, like and regex forms select labels; loc explicitly combines a value condition with a column list.
Does filter rename columns?
No. It retains selected labels. Use rename for an explicit name change.
What does regex r"_pl$" select?
In the example schema it selects names ending in _pl: sales_pl and cost_pl.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
