I have a DataFrame defined by:
df = pd.DataFrame({ 'id':[1,2,3], 'activity':['A1', 'A2', 'A2'], 'prep_hours':[None,None,1], 'delivery_hours':[10,10,15]}) I want to create a column total_hours which is the sum of all columns matching the pattern *_hours
For the time being, I simply add the columns I want into a new column:
df.fillna(0, inplace=True) df['total_hours'] = df['prep_hours'] + df['delivery_hours'] But it does not scale easily. For the sake of example, I only have 2 columns called *_hours but in the real DataFrame, it contains more than 30 columns that should be added.
Is there a smarter way to do it?