I have a DataFrame like this:
df =
Col1 Col2 T3 T5
------------------
28 34 11 22
45 589 33 66
对于每一行,我想要将以Col
开头的列的总值相加。是否有比下面展示的更优雅且更快的方法?
df['total'] = 0
for index, row in df.iterrows():
total_for_row = 0
for column_name, column in df.transpose().iterrows():
if 'Col' in column_name:
total_for_row = total_for_row + row[column_name]
row['total'] = total_for_row
df[df.columns[df.columns.str.contains('Col')]].sum(axis=1)
应该可以工作。 - EdChum