To put the average in the last column, it is necessary:
Turn first row into header
Calculate the mean on the vertical axis (axis=0
)
Indicate that you want the (average) calculation only with the numerical values, otherwise the Labels (e.g., 'consumption1') will interfere in the calculation.
Below is how to insert the media column':
# Cria o DataFrame
df = pd.DataFrame({'cliente':[1,2,3],'consumo1':[10,11,45],'consumo2':[12,20,10],'consumo3':[13,45,18]})
# Transpõe
df_transp = df.T
# Saída
df_transp
0 1 2
cliente 1 2 3
consumo1 10 11 45
consumo2 12 20 10
consumo3 13 45 18
# Seta o nome das colunas para os valores da primeira linha
df_transp.columns = df_transp.iloc[0]
# Define os dados começando a partir da segunda linha
df_transp = df_transp[1:]
# Saída
df_transp
cliente 1 2 3
consumo1 10 11 45
consumo2 12 20 10
consumo3 13 45 18
# AQUI => Cria a coluna 'media' com os valores
# da média (apenas para valores numéricos) calculados
# no eixo vertical (axis=0)
df_transp.loc[:,'media'] = df_transp.mean(numeric_only=True, axis=0).values
# Saída
df_transp
cliente 1 2 3 media
consumo1 10 11 45 11.666667
consumo2 12 20 10 25.333333
consumo3 13 45 18 24.333333
Managed to remedy my doubts, and still managed to apply in other situations, thank you!!
– Matheus Frota