Search code examples
pandasmathpandas-groupbylogarithm

pandas group by and log add


My first dataframe is this :

enter image description here

I need to make a group by date AND make a log add on column "SEL"

So, I can do the group by date like this :

df.groupby([df.index.date])["SEL"]

But the formula is

> 10*math.log10(10**(df["SEL"]/10).sum())

Anyone can help me please?

First i used :

temp=0
for index,row in df.iterrows():
    temp+=10**(row["SEL"]/10)
sumadd=10*math.log10(temp)

Is this the only way?

That's a example of output for several days :

enter image description here


Solution

  • Create new column with GroupBy.transform and set missing values to all column per date without last by Series.mask:

    dates = df.index.normalize()
    df['new'] = (10*np.log10((10**(df["SEL"]/10)).groupby(dates).transform('sum'))
                       .mask(dates.duplicated(keep='last')))