在桶中分配价值 [英] distribute value in buckets

查看:44
本文介绍了在桶中分配价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在 DF 之下,我有一个输入 number=4 要均匀插入到不同的小时桶中.

Consider below DF, I have an input number=4 to be inserted evenly in different hour buckets.

    p_hourly            mins
0   2020-09-10 07:00:00 60.0
1   2020-09-10 08:00:00 60.0
2   2020-09-10 09:00:00 60.0
3   2020-09-10 10:00:00 0.0

这意味着,必须将 4 平均分成 4 个 p_hourly 桶.让我们为每个存储桶设置 i=1.现在对于每次迭代,我们必须检查哪个 mins 值最高,并将 1 添加到其特定的 i 桶 并划分 mins 由新的 i bucket 值组成.在一行的情况下,所有 mins 值都相同,我们只需取第一个!

This means, 4 has to be divided evenly into 4 p_hourly buckets. Let's have i=1 for each bucket. Now for each iteration, we have to check which mins value is highest and add 1 to its specific i bucket and divide mins by the new i bucket value. In case of a row, all mins values are the same, we simply take the first one!

我在excel中找到了一个解决方案,但需要使用python来完成!

I figured out a solution in excel, but need to use python to get this done!

推荐答案

def get_ratios(df_in, val):
    '''
Dividing into Ratios 
'''
    df_in['x'] = 1
    df_in['new_mins']=df_in.mins
    for i in range(1,val+1):
        
        df_in['new_mins'][df_in.new_mins.idxmax(axis = 0)] = df_in.mins.iloc[df_in.new_mins.idxmax(axis = 0)]/df_in['x'][df_in.new_mins.idxmax(axis = 0)]
        df_in['x'][df_in.new_mins.idxmax(axis = 0)] = df_in['x'][df_in.new_mins.idxmax(axis = 0)] + 1
        
    df_in.x=df_in.x-1

    return df_in.x.values

这篇关于在桶中分配价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆