I sorted my CSV file to make some calculates. Python 2.7
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
What I need:
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)
Filling in the real data, the first two lines would be:
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
Remember I have more than 10,000 rows son I need a multiple form
The new CSV should show me this:
Cliente,Auth_domain,Sitio,Country,Recomendation_ECPM
FF,@ff,ff_Color,Afganistán,0.5
FF,@ff,ff_Color,Alemania,0.34
I'm not sure I have the correct
setval
or compare_val
But the pipeline regardless of those uses sort, group_by, and transform. Because we'll compare the edges to nan
( shift(-1)
on first, and shift(1)
on the end), we have to remove them at the end.
# build data
from StringIO import StringIO
import pandas as pd
df = pd.read_csv(StringIO("""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
""")).sort_values(by='Fecha')
# functions to parse
def compare_val(cur,past,future):
if cur <= past:
cur_adj = cur * .8
if cur_adj >= past:
return(past)
else:
return(cur_adj)
else:
if future >= past:
return(past)
else:
return(future)
def setval(v):
cur, past, future = v, v.shift(-1), v.shift(1)
v = [ compare_val(*x) for x in zip(cur,past,future)]
return(v)
# do the work
df['Recomendation_ECPM'] = df.\
groupby(['Cliente','Auth_domain','Sitio',"Country"])['ECPM_medio'].\
transform(setval)
df[ pd.notna(df['Recomendation_ECPM']) ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.