按位置在特定 pandas 列中应用 if else 条件

Apply if else condition in specific pandas column by location

我正在尝试按位置将条件应用于 pandas 列,但我不太确定如何操作。这是一些示例数据:

 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).
  1. PopDF['Pop2']= PopDF['Pop2'].iloc[:(remainder)]-1
    
  2. PopDF['Pop2'].iloc[(remainder):] = PopDF['Pop'].iloc[(remainder):]
    

第一行在正确的位置减去 1,但是,剩余的单元格变为 NaN。第二行代码不起作用——错误是:

ValueError: Length of values (1) does not match length of index (8)

不是选择前 N 行并减去它们,而是减去整个列并只分配它的前 6 个值:

df.loc[:remainder, 'Pop2'] = df['Pop2'] - 1

输出:

>>> 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