Comparison of CSV grouped rows. Python

1

Sort my CSV to be able to do some math operations with if - else

import pandas as pd
df = pd.read_csv('Cliente_x_Pais_Sitio.csv', sep=',')
df1 = df.sort_values(by=['Cliente','Auth_domain','Sitio',"Country"])
df1.to_csv('test.csv')

CSV data (test.csv):

Cliente,Fecha,Auth_domain,Sitio,Country,ECPM_medio
FF,15/12/2017,@ff,ff_Color,Afganistán,0.53
FF,15/01/2018,@ff,ff_Color,Afganistán,0.5
FF,15/01/2017,@ff,ff_Color,Alemania,0.34
FF,15/12/2017,@ff,ff_Color,Alemania,0.38
FF,15/01/2018,@ff,ff_Color,Alemania,0.37

These are the if - else you need, it should be noted that what is in parentheses are the values of the column ECPM_Medio with respect to the dates. Basically, for each Cliente, Auth_domain, Sitio, Country I compare the values that there are ECPM_Medio of each Date

if (15/12/2017 ECPM) ≤ (15/01/2018 ECPM):
    if ((15/12/2017 ECPM)*0.8) ≥ (15/01/2017 ECPM):
        r = (15/01/2017 ECPM)
    else:
        r = ((15/12/2017 ECPM)*0.8)
else:
    if (15/01/2018 ECPM) ≥ (15/01/2017 ECPM):
        r = (15/01/2017 ECPM)
    else:
        r = (15/01/2018 ECPM)

An Example with the values:

if 0.53 ≤ 0.5:
    if 0.5 ≥ 0: #if we don't have the cell value I would like to add a 0 True
        r = 0.5

I work with a lot of data so a formula for multiple groups and not manually write the data I need.

My new CSV should be something like this:

Cliente,Auth_domain,Sitio,Country,Recomendation_ECPM
FF,@ff,ff_Color,Afganistán,0.5
FF,@ff,ff_Color,Alemania,0.34
    
asked by Martin Bouhier 17.01.2018 в 15:07
source

0 answers