我有一个像这样的DataFrame:
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
答案 0 :(得分:0)
试试这个
idx = df.columns.str.startswith('Col')
df['total'] = df.iloc[:,idx].sum(axis=1)