Duplicate row based on value in different column

前端 未结 3 1270
感动是毒
感动是毒 2020-12-06 05:07

I have a dataframe of transactions. Each row represents a transaction of two item (think of it like a transaction of 2 event tickets or something). I want to duplicate each

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 05:52

    Answer by using repeat

    df.loc[df.index.repeat(df.Quantity)]
    Out[448]: 
      Price City Quantity
    1    20  NYC        2
    1    20  NYC        2
    2    30  NYC        2
    2    30  NYC        2
    3     5  NYC        2
    3     5  NYC        2
    4   300   LA        2
    4   300   LA        2
    5    30   LA        2
    5    30   LA        2
    6   100   LA        2
    6   100   LA        2
    

提交回复
热议问题