我正在尝试按位置将条件应用于熊猫列,但不太确定如何。以下是一些示例数据:
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):]
第一行用于在正确的位置减去 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句