Home > other >  Select only columns of interest dynamically
Select only columns of interest dynamically

Time:06-04

I am creating a dynamic pandas dataframe from some values. The dataframe dynamically obtains the name of the columns. Each column name has a numeric sequence that concatenates the name:

Name_Column_1, Name_Column_2, etc.

In context, each column name is a category of the data. The data can repeat its category, that's why I concatenated a number in its name

Name_1, Email_1, Phono_1, Name_2, Email_2, Phono_2, etc

What I want is to keep only the columns that interest me regardless of their number. To explain myself, in SQL language it would be something like:

SELECT * FROM TABLE WHERE column_name LIKE 'Email%'

I've been looking for a way to do it, but I can't find an answer.

Do you recommend any features please?

From already thank you very much.

Regards

CodePudding user response:

IIUC, you can try DataFrame.filter

out = df.filter(like='Email')

If you are interested in the number

out = df.filter(regex='Email_[1-5]')
  • Related