Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a subset of a DataFrame depending on column name

I have a pandas DataFrame called timedata with different column names, some of which contain the word Vibration, some eccentricity. Is is possible to create a dataframe of just the columns containing the word Vibration?

I have tried using

vib=[]
for i in timedata:
    if 'Vibration' in i:
        vib=vib.append(i)

to then create a DataFrame based on the indicies of these columns. This really does not seem like the most efficient way to do it and I'm sure there must be something simple to do with list comprehension.

EDIT

Dataframe of form:

df = DataFrame({'Ch 1:Load': randn(10), 'Ch 2:Vibration Brg 1T ': randn(10), 'Ch 3:Eccentricity Brg 1H ': randn(10), 'Ch 4:Vibration Brg 2T ': randn(10)})

Sorry I'm having a slow day! thanks for any help

like image 845
user2761786 Avatar asked Jan 03 '14 11:01

user2761786


1 Answers

Something like this to manually select all columns with the word "Vibration" in it:

df[[col for col in df.columns if "Vibration" in col]]

You can also do the same with the filter method:

df.filter(like="Vibration")

If you want to do a more flexible filter, you can use the regex option. E.g. to look if "Vibration" or "Ecc" is in the column name:

df.filter(regex='Ecc|Vibration')
like image 87
joris Avatar answered Sep 21 '22 17:09

joris