3

I am new to pandas and I can add to cumsum as

df.cumsum(axis=1)

   y0  y1  y2
0   2   3   4
1   2   2   3
2   0   0   0
3   1   2   3
   y0  y1  y2
0   2   5   9
1   2   4   7
2   0   0   0
3   1   3   6

But is there way to perform on only first 2 columns i.e. skip y2?

user2661518
  • 2,677
  • 9
  • 42
  • 79

2 Answers2

5

You need to exclude y2, find cumsum and concat y2 back.

pd.concat([df[['y0', 'y1']].cumsum(axis=1),df['y2']], axis=1)

Output:

    y0  y1  y2
0   2   5   4
1   2   4   3
2   0   0   0
3   1   3   3
harvpan
  • 8,571
  • 2
  • 18
  • 36
5

You can also use .loc to select only the columns you care about.

cols = ['y0', 'y1']
df.loc[:, cols] = df.loc[:, cols].cumsum(axis=1)

Output

   y0  y1  y2
0   2   5   4
1   2   4   3
2   0   0   0
3   1   3   3

loc is a flexible way to slice a DataFrame and in general follows the format:

.loc[row_labels, column_labels]

where an : can be used to indicate all rows, or all_columns.

ALollz
  • 57,915
  • 7
  • 66
  • 89
  • Thanks:) Could you add a quick explanation of what that loc function actually does? With the `:` and everything – Greedo May 12 '21 at 15:50
  • 1
    @Greedo I added some information, but there's a better lengthy description https://stackoverflow.com/questions/44890713/selection-with-loc-in-python – ALollz May 12 '21 at 15:54