I am trying to apply a condition to a pandas column by location and am not quite sure how. Here is some sample data:
 data = {'Pop': [728375, 733355, 695395, 734658, 732811, 789396, 727761, 751967],
    'Pop2': [728375, 733355, 695395, 734658, 732811, 789396, 727761, 751967]}
 PopDF = pd.DataFrame(data)
 remainder = 6
#I would like to subtract 1 from PopDF['Pop2'] column cells 0-remainder.
#The remaining cells in the column I would like to stay as is (retain original pop values).
- 
PopDF['Pop2']= PopDF['Pop2'].iloc[:(remainder)]-1
 - 
PopDF['Pop2'].iloc[(remainder):] = PopDF['Pop'].iloc[(remainder):]
 
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:
ValueError: Length of values (1) does not match length of index (8)
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:
df.loc[:remainder, 'Pop2'] = df['Pop2'] - 1
Output:
>>> df
      Pop    Pop2
0  728375  728374
1  733355  733354
2  695395  695394
3  734658  734657
4  732811  732810
5  789396  789395
6  727761  727760
7  751967  751967