简体   繁体   中英

loop a list of columns to sum and average

I have tried to develop a funciton that can divide first and then sum up the result from dividing by checking a list of string. But I always get the NaN after sum. Please let me know how to fix it. Thanks.

targetlist = ['Comp_date_1_180_Sum_add_count',
                'Base_date_1_180_Sum_add_count']
columnlist = ['Sum_add_count']
def div(df, targetlist, columnlist, n):
    for i in targetlist:
        df[i] = df[i].fillna(0)
        df[i + "_daily_avg"] = df[i] / n
        for j in columnlist:
            df[j + "_daily_avg"] = df.loc[:, df.columns.str.contains('|'.join(j + "_daily_avg"))].sum(axis=1)
    return df

Smpale Data

Comp_date_1_180_Sum_add_count Base_date_1_180_Sum_add_count
21898.0                        27542.0
2517.0                         3727.0
8566.0                         8130.0

Expected output

Comp_date_1_180_Sum_add_count_daily_avg | Base_date_1_180_Sum_add_count | Sum_add_count_daily_avg |
Comp_date_1_180_Sum_add_count_daily_avg = Comp_date_1_180_Sum_add_count/ n
Base_date_1_180_Sum_add_count = Base_date_1_180_Sum_add_count/ n
Sum_add_count_daily_avg = Average(Comp_date_1_180_Sum_add_count_daily_avg,Base_date_1_180_Sum_add_count)

Just run your code and it worked fine. So I suppose the problem in your data table. Could you try to run it with

df[i + "_daily_avg"] = df[i].astype(float) / n

instead of

df[i + "_daily_avg"] = df[i] / n

in div()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM