3

I am trying to generate new columns using the existing columns in the data frame. My dataframe has 60 columns . To need to generate 60 new columns based on the data in the data frame. These columns should only contain the sum. E.g

Index column_a column_b month 
0.         1.     3.     Jan 
1.         2.     4.     Feb
2.         3.     4      Match
Index column_a column_b month  sum_a sum_b
0.         1       2.    Jan.   6.      10
1.         2.      4.    Feb.   6.      10
2.         3.      4     Match. 6.      10

Keep in mind that I need to iterate this for 60 columns. Would appreciate any help. Thanks

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

first create the list of columns on which you have to calculate sum then iterate on those columns

list_of_cols =['col_a','col_b']
for i in list_of_cols:
    name = 'sum_'+ i
    df[name] = df[i].sum()

I hope it would solve your problem

tawab_shakeel
  • 3,701
  • 10
  • 26