Skip to content Skip to sidebar Skip to footer

How To Select All Columns That Start With "durations" Or "shape"?

How to select all columns that have header names starting with 'durations' or 'shape'? (instead of defining a long list of column names). I need to select these columns and substit

Solution 1:

You could use str methods of dataframe startwith:

df = data[data.columns[data.columns.str.startwith('durations') | data.columns.str.startwith('so')]]
df.fillna(0)

Or you could use contains method:

df = data.iloc[:, data.columns.str.contains('durations.*'|'shape.*') ]
df.fillna(0)

Solution 2:

I would use the select method:

df.select(lambda c: c.startwith('durations') or c.startswith('shape'), axis=1)

Solution 3:

Use my_dataframe.columns.values.tolist() to get the column names (based on Get list from pandas DataFrame column headers):

column_names = [x for x in data.columns.values.tolist() if x.startswith("durations") or x.startswith("shape")]

Solution 4:

A simple and easy way

data[data.filter(regex='durations|shape').columns].fillna(0)

Sample Screenshot

enter image description here

Post a Comment for "How To Select All Columns That Start With "durations" Or "shape"?"