Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Decrease pandas series value if it is higher than the mean

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading