im having a trouble with some work. I need to make these:
tarjeta_maestro.groupby(by=['Cod Marca Producto', 'Cod Comercio']).agg({'Nro Liquidacion': 'max',
'VENTAS C/DESCUENTO CONTADO': 'sum',
'ARANCEL': 'sum',
'IVA CRED.FISC.COMERCIO S/ARANC 21,00%':'sum',
'Iva 21':'sum',
'RETENCION IVA': 'sum',
'PER B.A.I.BR.DN.01/04': 'sum',
'RETENCION IMP.GANANCIAS': 'sum',
'RETENCION ING.BRUTOS': 'sum',
'RETENCION ING.BRUTOS SIRTAC': 'sum',
'TOTAL DEDUCCIONES': 'sum',
'TOTAL LIQUIDACION': 'sum',
'Diferencia' : 'sum' })
My problem is that I need to pass a parameter 'max', and the rest of the columns 'sum', but I can't find a way to achieve it without specifying the function corresponding to each column. I need to apply this to several more df with different columns I imagine that it should be possible to do it in a simple way, but I couldn't find the solution. Could you guide me to solve this problem?
CodePudding user response:
You can use dictionary comprehension to generate the agg
function
# You can access desired columns with df.columns
# if you have many target columns and they are continuous
columns = ['Nro Liquidacion', 'VENTAS C/DESCUENTO CONTADO', 'ARANCEL', 'IVA CRED.FISC.COMERCIO S/ARANC 21,00%', 'Iva 21', 'RETENCION IVA', 'PER B.A.I.BR.DN.01/04', 'RETENCION IMP.GANANCIAS', 'RETENCION ING.BRUTOS', 'RETENCION ING.BRUTOS SIRTAC', 'TOTAL DEDUCCIONES', 'TOTAL LIQUIDACION', 'Diferencia']
max_columns = ['Nro Liquidacion']
d = {col: ('max' if col in max_columns else 'sum') for col in columns}
tarjeta_maestro.groupby(by=['Cod Marca Producto', 'Cod Comercio']).agg(d)
CodePudding user response:
You can insert tarjeta_maestro.columns.drop('Nro Liquidacion')[0] : 'sum'
inside agg
:
tarjeta_maestro.groupby(by=['Cod Marca Producto', 'Cod Comercio']).\
agg({'Nro Liquidacion': 'max',\
tarjeta_maestro.columns.drop('Nro Liquidacion')[0] : 'sum'})