2

I think this should be a simple problem, but I can't find a solution.

Within a subset of rows in a dataframe, I need to decrement the value of each item in a column by 1. I have tried various approaches, but the values continue to be unchanged. Following another entry on SO, I tried

def minus1(x):
    x =x-1
    return x

pledges[pledges.Source == 'M0607'].DayOFDrive = pledges[pledges.Source == 'M0607'].DayOFDrive.map(minus1)

When I typed

pledges[pledges.Source == 'M0607'].DayOFDrive

to check it, the original unchanged data came back. I have also tried

pledges[pledges.Source == 'M0607'].DayOFDrive = pledges[pledges.Source == 'M0607'].DayOFDrive-1

which also does nothing.

How can I reduce all the values in a column by 1 for a subset of rows ?

chrisfs
  • 6,182
  • 6
  • 29
  • 35

1 Answers1

3

If this returns the data you want to modify:

pledges[pledges.Source == 'M0607'].DayOFDrive

Then try modifying it this way:

pledges[pledges.Source == 'M0607'].DayOFDrive -= 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • I did try that and it didn't work. I solved it by reconstructing the dataframe from scratch, but still I would like to figure out the way to do this. – chrisfs Jun 23 '13 at 02:22
  • Looks promising. I will try it out later. Thanks! – chrisfs Jun 23 '13 at 02:38