distribute value in buckets

2024/9/21 7:05:18

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

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!

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

enter image description here

Answer
def get_ratios(df_in, val):'''
Dividing into Ratios 
'''df_in['x'] = 1df_in['new_mins']=df_in.minsfor 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)] + 1df_in.x=df_in.x-1return df_in.x.values
https://en.xdnf.cn/q/120290.html

Related Q&A

for loop over list break and continue

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entr…

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…

How can I filter the domains served by a CDN from a list of domain names?

I have a list of domains and I need to filter the domains served by a CDN(Content Delivery Network). I am going to use python script to do that. At the first I was thinking I can identify them from the…

Convert int(round(time.time())) to C# [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to iterate over all elements of a 2D matrix using only one loop using python

I know you can iterate over a 2d matrix using two indexes like this: import numpy as npA = np.zeros((10,10))for i in range(0,10):for j in range(0,10):if (i==j):A[i,j] = 4Is there a way of doing this us…

Parse table names from a bunch SQL statements

I have an table with thousands of SQL statements in a column called Queries. Any ideas on how to get just the table names from the statements by using a regular expression?

click multiple buttons with same class names in Python

This a column in a table this column contains buttons, on pressing each buttons a pdf is downloadedThe buttons have the same class names and I want to click on all the buttons.This is what I did, but i…

Python equivalent to subset function in r [duplicate]

This question already has answers here:subsetting a Python DataFrame(6 answers)Closed 4 years ago.I dont know python at all but the project Im currently working on must be done using it, I have this r …

Force subprocess to use Python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python: Im making a simple calculator for class. Whats wrong with this code? [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Closed 7 months ago.My teacher requests me to make a calculator that can calculate a 15% tip on a submitted price. I…