You can use the str.startswith DataFrame method to give more consistent results:

In [11]: s = pd.Series(['a', 'ab', 'c', 11, np.nan])

In [12]: s
Out[12]:
0      a
1     ab
2      c
3     11
4    NaN
dtype: object

In [13]: s.str.startswith('a', na=False)
Out[13]:
0     True
1     True
2    False
3    False
4    False
dtype: bool

and the boolean indexing will work just fine (I prefer to use loc, but it works just the same without):

In [14]: s.loc[s.str.startswith('a', na=False)]
Out[14]:
0     a
1    ab
dtype: object

.

It looks least one of your elements in the Series/column is a float, which doesn't have a startswith method hence the AttributeError, the list comprehension should raise the same error...

Answer from Andy Hayden on Stack Overflow
🌐
Statology
statology.org › home › pandas: how to use startswith in query() method
Pandas: How to Use startswith in query() Method
April 24, 2024 - Often you may want to use the startswith() function within the query() method in pandas to filter for rows in a DataFrame where a column starts with a specific string.
Discussions

python - Select rows where value of column A starts with value of column B - Stack Overflow
Looking ahead, starting conversations, and seizing the moment ... 5 Pandas dataframe - Select rows where one column's values contains a string and another column's values starts with specific strings More on stackoverflow.com
🌐 stackoverflow.com
python - Pandas: select dataframe rows only if the values in a specific column start with - Stack Overflow
I have the following dataframe df1: X Y A B 0 484 408 10 3360 1 478 415 24 3365 2 504 452 31 yes ... More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
python - Pandas dataframe - Select rows where one column's values contains a string and another column's values starts with specific strings - Stack Overflow
I'm looking to select rows where state contains the word Traded and trading _book does not start with letters 'E','L','N' Test_Data = [('originating_system_id', ['RBCL', 'RBCL', 'RBCL','RBCL']), ... More on stackoverflow.com
🌐 stackoverflow.com
July 17, 2018
python - How to get rows of Pandas Dataframe where the column value starts with any of given characters - Stack Overflow
0 In Python, how do i read all columns where the values start with a certain string? 7 Select rows if string begins with certain characters in pandas More on stackoverflow.com
🌐 stackoverflow.com
🌐
David Hamann
davidhamann.de › posts › pandas: select rows that match a string
Pandas: Select rows that match a string | David Hamann
June 26, 2017 - import pandas as pd #create sample data data = {'model': ['Lisa', 'Lisa 2', 'Macintosh 128K', 'Macintosh 512K'], 'launched': [1983,1984,1984,1984], 'discontinued': [1986, 1985, 1984, 1986]} df = pd.DataFrame(data, columns = ['model', 'launched', 'discontinued']) df · We want to select all rows where the column ‘model’ starts with the string ‘Mac’. df[df['model'].str.match('Mac')] We can also search less strict for all rows where the column ‘model’ contains the string ‘ac’ (note the difference: contains vs.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-select-data-from-a-pandas-dataframe-using-startswith
How to Select Data from a Pandas Dataframe using Startswith | Saturn Cloud Blog
May 1, 2026 - To select data from a Pandas Dataframe using startswith, we can use the str.startswith() method provided by Pandas. This method returns a Boolean series that indicates whether each string in the specified column starts with the specified prefix.
🌐
Skytowner
skytowner.com › explore › selecting_rows_starting_with_substring_in_pandas_dataframe
Selecting rows starting with substring in Pandas DataFrame
To select rows of a Pandas DataFrame starting with a specified substring we can use the str.startswith(~) method.
🌐
Medium
medium.com › @akaivdo › pandas-select-rows-from-a-dataframe-based-on-column-values-29aef08388ec
Pandas >> Select Rows From a DataFrame Based on Column Values | by NextGenTechDawn | Medium
May 6, 2023 - In this tutorial, we will talk about how to select rows based on column values or relations between columns in Pandas, use boolean indexing with logical operators or query methods, and methods using regular expressions like str.match(), str.contains() and python re module.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › check-if-a-column-starts-with-given-string-in-pandas-dataframe
Check if a column starts with given string in Pandas DataFrame? - GeeksforGeeks
April 28, 2025 - Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator. Code #1 : Selecting all the rows from the given dataframe in which 'Percentage' is greater than 80 using basic method.
🌐
Medium
medium.com › @akaivdo › how-to-select-rows-containing-specified-string-7cbba8ffcac4
Pandas >> How to Select Rows Containing Specified String | by NextGenTechDawn | Medium
February 12, 2022 - For example, we can use.*V.* to select students who are interested in Violin and Volunteering. By default regex mode is open. If we want to turn it off, we can specify regex=False. Use str.startswith() to find rows whose column starts with a pattern.
🌐
Bobby Hadz
bobbyhadz.com › blog › pandas-select-all-columns-starting-with-given-string
Select all Columns starting with a given String in Pandas | bobbyhadz
April 12, 2024 - Use the str.startswith() method to check if each column name starts with the given string. Optionally use bracket notation to select the matching columns. ... Copied!import pandas as pd df = pd.DataFrame({ 'Employee_ID': [1, 2, 3, 4], ...
🌐
Pandas
pandas.pydata.org › docs › getting_started › intro_tutorials › 03_subset_data.html
How do I select a subset of a DataFrame? — pandas 3.0.6 documentation
The inner square brackets define a Python list with column names, whereas the outer square brackets are used to select the data from a pandas DataFrame as seen in the previous example. ... The selection returned a DataFrame with 891 rows and 2 columns.
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to select rows from pandas dataframe based on column values
How To Select Rows From Pandas DataFrame Based on Column Values | Towards Data Science
January 20, 2025 - Selecting rows from a DataFrame ... can do with pandas. In today's article we are going to discuss how to perform row selection over pandas DataFrames whose column(s) value is: ... A member of an iterable (e.g. a list) Additionally, we will discuss how to combine multiple conditions together. Before start discussing ...
Top answer
1 of 12
262

Just perform a list comprehension to create your columns:

In [28]:

filter_col = [col for col in df if col.startswith('foo')]
filter_col
Out[28]:
['foo.aa', 'foo.bars', 'foo.fighters', 'foo.fox', 'foo.manchu']
In [29]:

df[filter_col]
Out[29]:
   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu
0     1.0         0             0        2         NA
1     2.1         0             1        4          0
2     NaN         0           NaN        1          0
3     4.7         0             0        0          0
4     5.6         0             0        0          0
5     6.8         1             0        5          0

Another method is to create a series from the columns and use the vectorised str method startswith:

In [33]:

df[df.columns[pd.Series(df.columns).str.startswith('foo')]]
Out[33]:
   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu
0     1.0         0             0        2         NA
1     2.1         0             1        4          0
2     NaN         0           NaN        1          0
3     4.7         0             0        0          0
4     5.6         0             0        0          0
5     6.8         1             0        5          0

In order to achieve what you want you need to add the following to filter the values that don't meet your ==1 criteria:

In [36]:

df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]]==1]
Out[36]:
   bar.baz  foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu nas.foo
0      NaN       1       NaN           NaN      NaN        NaN     NaN
1      NaN     NaN       NaN             1      NaN        NaN     NaN
2      NaN     NaN       NaN           NaN        1        NaN     NaN
3      NaN     NaN       NaN           NaN      NaN        NaN     NaN
4      NaN     NaN       NaN           NaN      NaN        NaN     NaN
5      NaN     NaN         1           NaN      NaN        NaN     NaN

EDIT

OK after seeing what you want the convoluted answer is this:

In [72]:

df.loc[df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]] == 1].dropna(how='all', axis=0).index]
Out[72]:
   bar.baz  foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu nas.foo
0      5.0     1.0         0             0        2         NA      NA
1      5.0     2.1         0             1        4          0       0
2      6.0     NaN         0           NaN        1          0       1
5      6.8     6.8         1             0        5          0       0
2 of 12
117

Now that pandas' indexes support string operations, arguably the simplest and best way to select columns beginning with 'foo' is just:

df.loc[:, df.columns.str.startswith('foo')]

Alternatively, you can filter column (or row) labels with df.filter(). To specify a regular expression to match the names beginning with foo.:

>>> df.filter(regex=r'^foo\.', axis=1)
   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu
0     1.0         0             0        2         NA
1     2.1         0             1        4          0
2     NaN         0           NaN        1          0
3     4.7         0             0        0          0
4     5.6         0             0        0          0
5     6.8         1             0        5          0

To select only the required rows (containing a 1) and the columns, you can use loc, selecting the columns using filter (or any other method) and the rows using any:

>>> df.loc[(df == 1).any(axis=1), df.filter(regex=r'^foo\.', axis=1).columns]
   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu
0     1.0         0             0        2         NA
1     2.1         0             1        4          0
2     NaN         0           NaN        1          0
5     6.8         1             0        5          0