-3

Is there any smart way to split (explode) a row into multiple rows without using explode, at the same time intelligently divide a column value equally?

Suppose I have a data like this:

date quantity  days
5-mar      10     1
8-mar      30     3
9-Mar      10     1

and the resultant data ought to be like this:

enter image description here

I have tried to use assign - but to no avail. Issit that the only way to do it is through iterrows(), loop through it and explicitly create new rows?

P/s: Due to certain restrictions and I have to use pandas <0.25, so I can't really use .explode()

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
CozyAzure
  • 8,280
  • 7
  • 34
  • 52
  • 6
    [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – It_is_Chris Jun 07 '21 at 13:18
  • 1
    why do all dates just have values of 10 and 1 in the other columns? and where did the two new dates come from? where did the 30/3 go? do you just want a df with all dates, and a constant value in the other two columns? – scotscotmcc Jun 07 '21 at 13:22
  • No, the reason is because the missing two days are weekends, and so the `8 March` has a cumulative of 3 days worth of quantity. – CozyAzure Jun 07 '21 at 13:23

1 Answers1

1

Use Index.repeat with DataFrame.loc for new rows by column days and then need some processing for all columns - division for quantity, remove days by counter by GroupBy.cumcount and set days to 1:

df['date'] = pd.to_datetime(df['date'], format='%d-%b')
df1 = df.loc[df.index.repeat(df['days'])]


df1['quantity'] = df1['quantity'].div(df1['days'])
df1['date'] -= pd.to_timedelta(df1.groupby(level=0).cumcount(ascending=False), unit='d')
df1['days'] = 1

df1['date'] = df1['date'].dt.strftime('%d-%b')
print (df1)
     date  quantity  days
0  05-Mar      10.0     1
1  06-Mar      10.0     1
1  07-Mar      10.0     1
1  08-Mar      10.0     1
2  09-Mar      10.0     1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252