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