I have data similar to this:
Here, I have used the python function ffill() to fill the last finite value to the next nan cells.That's why the quantity is same for all the dates under a flag. Count is just the number of rows where flag is same.
Now, I need to split the quantity column like this:
So here 6 is divided as 2+2+1+1 because 6 needs to be divided in 4 rows. And similarly, 5 as 1+1+1+1+1 because I have more number of rows(9) than the value(5). So I can evenly distribute 5 as 1's in starting 5 rows. How can I do this in python?
First define a function with the logic to split a number x
in to n
parts.
def get_split(x, n):
if x < n:
return [1]*x + [0]*(n-x)
q = x//n
r = x%n
c = n-r
if r == 0:
return [q]*n
else:
return [q+1 if i>=c else q for i in range(n)]
This function will return a list of the split
Then group the df
using the df['flag']
column and for each group get the split using the above function and update the df['new qty']
column.
df['new qty'] = 0
groups = df.groupby('flag')
for key, grp in groups:
x, n = grp.head(1)[['qty', 'count']].values[0]
splits = sorted(get_split(x, n), reverse=True)
j=0
for i, row in grp.iterrows():
df.loc[i, 'new qty'] = splits[j]
j+=1
print(df)
Result:
Date Desciption qty flag count new qty
0 2019-09-18 A 6 3 4 2
1 2019-09-19 A 6 3 4 2
2 2019-09-20 A 6 3 4 1
3 2019-09-21 A 6 3 4 1
4 2019-09-22 A 5 7 9 1
5 2019-09-23 A 5 7 9 1
6 2019-09-24 A 5 7 9 1
7 2019-09-25 A 5 7 9 1
8 2019-09-26 A 5 7 9 1
9 2019-09-27 A 5 7 9 0
10 2019-09-28 A 5 7 9 0
11 2019-09-29 A 5 7 9 0
12 2019-09-30 A 5 7 9 0
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.