I have 2 columns called eta and theta in a pandas data frame.
I’m trying to select the column called eta only.
So far I have tried
df.filter(like='eta')
…however this selects both eta and theta.
I’ve also tried pattern, but this is an R command:
df.filter(pattern='^eta')
>Solution :
Use the regex parameter of filter:
df.filter(regex=r'^eta')
Make sure to escape regex characters if needed, for instance if the columns start with eta.:
df.filter(regex=r'^eta\.')
If you have a doubt:
import re
target = 'eta'
df.filter(regex=fr'^{re.escape(target)}')