How to select all columns starting with "duration" or "form"?

How to select all columns with heading names starting with "duration" or "form"? (instead of defining a long list of column names). I need to select these columns and replace the empty fields with 0.

column_names = ['durations.blockMinutes_x', 'durations.scheduledBlockMinutes_y'] data[column_names] = data[column_names].fillna(0) 
0
source share
4 answers

You can use str dataframe startwith methods:

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

Or you can use the contains method:

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

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

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

I would use the select method:

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

0
source

Simple and easy way

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

Sample screenshot

enter image description here

0
source

All Articles