1

I have made the column "Turno" on the df3 using 3 validation to classify into "Turno_PM", "Turno_AM" or "N/A", but I want to know if exist an "easies way" to reach the same result, like a "cycle for" with if/elif/else or something like that.

Here the code that I have used.

from databricks import koalas as ks
from databricks.koalas.config import set_option, reset_option
set_option("compute.ops_on_diff_frames", True)

#Turno PM
kdf.loc[(kdf['dot_agencia_origen'] == 'AGENCIA RM') & (kdf['dot_agencia_destino']!='AGENCIA RM') | (kdf['dot_agencia_origen'] == 'AGENCIA VALPARAISO') & (kdf['dot_agencia_destino']!='AGENCIA RM') & (kdf['dot_agencia_destino']!='AGENCIA VALPARAISO') | (kdf['dot_agencia_origen'] == 'AGENCIA RANCAGUA') & (kdf['dot_agencia_destino']!='AGENCIA RM') & (kdf['dot_agencia_destino']!='AGENCIA RANCAGUA'),'Turno']= 'Turno_PM'


#Turno AM
kdf.loc[(kdf['dot_agencia_origen'] == 'AGENCIA RM') & (kdf['dot_agencia_destino']=='AGENCIA RM') | (kdf['dot_agencia_origen'] == 'AGENCIA VALPARAISO') & (kdf['dot_agencia_destino']=='AGENCIA RM')|(kdf['dot_agencia_origen'] == 'AGENCIA RANCAGUA') & (kdf['dot_agencia_destino']=='AGENCIA RM'),'Turno']='Turno_AM'

#Regiones
kdf.loc[(df3['Turno'].isnull()),'Turno']='Regiones'

1 Answers1

0

Another solution -> construct the condition:

# Turno PM
cond1  = (df3['dot_agencia_destino'] != 'AGENCIA RM')
cond2  = False
cond2 |= (df3['dot_agencia_origen'] == 'AGENCIA RM')
cond2 |= (df3['dot_agencia_origen'] == 'AGENCIA VALPARAISO') & (df3['dot_agencia_destino']!='AGENCIA VALPARAISO')
cond2 |= (df3['dot_agencia_origen'] == 'AGENCIA RANCAGUA')   & (df3['dot_agencia_destino']!='AGENCIA RANCAGUA')
cond = cond1 & cond2
df3.loc[cond, 'Turno'] = 'Turno_PM'

# Turno AM
cond = True
cond &= (df3['dot_agencia_destino'] == 'AGENCIA RM')
cond &= (df3['dot_agencia_origen'].isin(['AGENCIA RM', 'AGENCIA VALPARAISO', 'AGENCIA RANCAGUA']))
df3.loc[cond, 'Turno'] = 'Turno_AM'

# N/A
df3['Turno'] = df3['Turno'].fillna('N/A')

Ferris
  • 5,325
  • 1
  • 14
  • 23