Home > database >  Duplicate rows according to col
Duplicate rows according to col

Time:07-06

Given table as below

  Destination  Capacity  TruckNum
0       dest1         1          1
1       dest2         1          2
2       dest3         2          2
3       dest4         2          2

From code

df=pd.DataFrame({"Destination":["dest1","dest2","dest3","dest4"], "Capacity":[1,1,2,2], "TruckNum":[1,2,2,2]})
  

I hope to duplicate row according to TruckNum, that is the desired outcome is:

  Destination  Capacity  
0       dest1         1           
1       dest2         1           
2       dest2         1           
3       dest3         2           
4       dest3         2           
5       dest4         2           
6       dest4         2           

Is there an easy way to achieve that?

CodePudding user response:

Try reindex with repeat

out = df.reindex(df.index.repeat(df.TruckNum))
Out[133]: 
  Destination  Capacity  TruckNum
0       dest1         1         1
1       dest2         1         2
1       dest2         1         2
2       dest3         2         2
2       dest3         2         2
3       dest4         2         2
3       dest4         2         2

CodePudding user response:

(df.assign(Capacity = df[['Capacity', 'TruckNum']]
  .apply(lambda x:[x[0]] * x[1], axis=1))
  .explode('Capacity').drop('TruckNum', axis=1))
Out[18]: 
  Destination Capacity
0       dest1        1
1       dest2        1
1       dest2        1
2       dest3        2
2       dest3        2
3       dest4        2
3       dest4        2
  • Related