0

Let's say I have a pandas DataFrame, df, as shown below and a list, l, also shown below. I want to select the columns from df for which any of the column names start with the strings in l. So in this case I want to get df[['word', 'hello1', 'hello2', 'hello3']]. Is there a quick way I can do this? I could loop through each element of the list but that could take a lot of time for larger DataFrames.

import pandas as pd
df = pd.DataFrame({
    'word': [13,4],
    'another': [1,4],
    'champ': [1,5],
    'hello1': [1,2],
    'hello2': [4,5],
    'hello3': [7,8]
})

l = ['word', 'hello']

#what I want to get:
   word  hello1 hello2  hello3
0   13      1      4    7
1   4       2      5    8

2 Answers 2

2

Since you want the column starting with given words, you can do:

df.loc[:, df.columns.str.match(f'^({"|".join(l)})')]

Output:

   word  hello1  hello2  hello3
0    13       1       4       7
1     4       2       5       8
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

df.loc[:,df.columns.str.startswith(tuple(l))]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.