cmf05 cmf05 - 4 months ago 11
Python Question

Python/Pandas Iterating through columns

I have a DataFrame which looks like this (with many additional columns)

age1 age2 age3 age 4 \
Id#
1001 5 6 2 8
1002 7 6 1 0
1003 10 9 7 5
1004 9 12 5 9


I am trying write a loop that sums each column with the previous ones before it and returns it to a new DataFrame.
I have started out, simply, with this:

New = pd.DataFrame()
New[0] = SFH2.ix[:,0]
for x in SFH2:
ls = [x,x+1]
B = SFH2[ls].sum(axis=1)
New[x] = B

print(New)


and the error I get is

ls = [x,x+1]

TypeError: Can't convert 'int' object to str implicitly


I know that int and str are different objects, but how can I overcome this, or is there a different way to iterate through columns?
Thanks!

Answer

It sounds like cumsum is what you are looking for:

In [5]: df
Out[5]: 
      age1  age2  age3  age4
Id#                         
1001     5     6     2     8
1002     7     6     1     0
1003    10     9     7     5
1004     9    12     5     9

In [6]: df.cumsum(axis=1)
Out[6]: 
      age1  age2  age3  age4
Id#                         
1001     5    11    13    21
1002     7    13    14    14
1003    10    19    26    31
1004     9    21    26    35