Sum all columns with a wildcard name search using Python Pandas
List comprehensions on columns allow more filters in the if
condition:
In [1]: df = pd.DataFrame(np.arange(15).reshape(5, 3), columns=['P1S1', 'P1S2', 'P2S1'])In [2]: dfOut[2]: P1S1 P1S2 P2S10 0 1 21 3 4 52 6 7 83 9 10 114 12 13 14In [3]: df.loc[:, [x for x in df.columns if x.startswith('P1')]].sum(axis=1)Out[3]: 0 11 72 133 194 25dtype: int64