0

I have the following dataframe:

        import pandas as pd
        df = pd.DataFrame({'Code': ['4338', '4466', '1024',
                                    '4338', '4338', '5548'], 
                           'Sensor': ['temp_sensor', 'temp_sensor', 'temp_sensor',
                                      'strain_gauge', 'light_sensor',
                                      'strain_gauge'],
                           'Material Qtd': [1, 3, 5, 9, 8, 1]})

       print(df)

       Code       Sensor        Material Qtd
       4338     temp_sensor          1
       4466     temp_sensor          3
       1024     temp_sensor          5
       4338     strain_gauge         9
       4338     light_sensor         8
       5548     strain_gauge         1

I'm doing some operations according to the df and creating the df2:

        df2 = df.groupby('Sensor').agg(Qtd_Code=('Code','count'), Sum_t=('Material Qtd', 
                                                                         'sum')).reset_index()

        print(df2)

             Sensor       Qtd_Code    Sum_t
         light_sensor        1          8
         strain_gauge        2          10
         temp_sensor         3          9

So, I create a new column that averages as follows:

        df2['Mean'] = df2['Sum_t'] / df2['Qtd_Code']

        print(df2)

           Sensor         Qtd_Code   Sum_t    Mean
         light_sensor        1         8       8.0
         strain_gauge        2         10      5.0
         temp_sensor         3         9       3.0

The results generated are as expected, but what I would like to do is return the average information generated to the original dataframe (df). That is, I would like the df output to be:

       Code       Sensor        Material Qtd      Mean
       4338     temp_sensor          1            3.0
       4466     temp_sensor          3            3.0
       1024     temp_sensor          5            3.0
       4338     strain_gauge         9            5.0
       4338     light_sensor         8            8.0
       5548     strain_gauge         1            5.0

Thank you.

Jane Borges
  • 552
  • 5
  • 14
  • 1
    try `df['Mean_'] = df.groupby('Sensor')['Material Qtd'].transform('mean')` ? – anky Jun 13 '20 at 15:00
  • 1
    No need for a second dataframe. You can apply this direcly onto your dataframe. See the code given by @Anky. For more samples, have a look at [this](https://cmdlinetips.com/2019/10/how-to-add-group-level-summary-statistic-as-a-new-column-in-pandas/) – JvdV Jun 13 '20 at 15:05

0 Answers0