How to save averages from a dataframe, a new column containing the averages of this dataframe using pandas?

1

Good evening, I'm having a problem trying to save average values of a df, in a single column. An example of how I wanted it to be was:

Dataframe

Inthisfirstpart,Ijustopenedmydataframeandtranspiredtogetthemean()valueswiththemean()function.

Maybe it's a bit of a beast doubt, but I'm really not able to put the mean values I got in that mean () function in a new column with those values. Can you do that? Right away, thank you !!

    
asked by anonymous 22.05.2018 / 01:28

1 answer

1

To put the average in the last column, you need to:

  • Transform first row into header

  • Perform the averaging on the vertical axis ( axis=0 )

  • Indicate that you want the calculation (of the mean) only with the numeric values, otherwise the labels (eg 'consumption1') will interfere with the calculation.

    Here's 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
    
        
  • 22.05.2018 / 03:10
    ___ ___ erkimt Materialize Javascript autocomplete ______ qstntxt ___

    I'm using the materialize framework. According to the documentation, I can use "autocomplete" in the form tag according to the Materialize Form link. My question, is this I want to create a javascript for autocomplete of extensions, example: @ gmail.com, @ outlook.com, @ hotmail.com. When the user says "@" this list appears. Below my code

    %pre%

    Javascript:

    %pre%     
    ______ azszpr300761 ___

    As I researched, autocomplete does not yet have a feature to show suggestions only after a certain user input. I saw that it's on the list of plugin upgrades.

    If I understand what you need, follow an alternative ...

    Note: The plugin's default operation has been modified.

    %pre% %pre% %pre%
        
    ___ Money Mask