Skip to content
Advertisement

Apply if else condition in specific pandas column by location

I am trying to apply a condition to a pandas column by location and am not quite sure how. Here is some sample data:

JavaScript
  1. JavaScript
  2. JavaScript

The first line works to subtract 1 in the correct locations, however, the remaining cells become NaN. The second line of code does not work – the error is:

JavaScript

Advertisement

Answer

Instead of selected the first N rows and subtracting them, subtract the entire column and only assign the first 6 values of it:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement