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



我正试图按位置将条件应用于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

最新更新