I am trying to decrease by 2.5% the value of all records that are higher than the mean in my pandas series.
I would like to solve the problem using the update method
price_per_city = {
"Bragança": 10.3,
"Braga": 10.6,
"Porto": 11.5,
"Aveiro": 12.3,
"Coimbra": 9.9,
"Leiria": 9.3,
"Lisboa": 12.1,
"Beja": 10.9,
"Évora": 11.4,
"Faro": 9.1
}
prcSerie = pd.Series(price_per_city)
prcSerie.update(prcSerie, prcSerie[prcSerie >=prcSerie.mean()])
print(prcSerie)
CodePudding user response:
Try with where
:
prcSerie = prcSerie.where(prcSerie.le(prcSerie.mean()), prcSerie.mul(0.975))
>>> prcSerie
Bragança 10.3000
Braga 10.6000
Porto 11.2125
Aveiro 11.9925
Coimbra 9.9000
Leiria 9.3000
Lisboa 11.7975
Beja 10.6275
Évora 11.1150
Faro 9.1000
dtype: float64
CodePudding user response:
Using update
for in place modification:
prcSerie.update(prcSerie.loc[prcSerie>=prcSerie.mean()].mul(0.975).round(2))
Output:
Bragança 10.30
Braga 10.60
Porto 11.21
Aveiro 11.99
Coimbra 9.90
Leiria 9.30
Lisboa 11.80
Beja 10.63
Évora 11.12
Faro 9.10
dtype: float64