Search code examples
pythonpandasdataframerowreplication

How to replicate rows based on value of a column in same pandas dataframe


I need to do some row replication. I have a dataframe that looks like this:

label count
HA1 5
HA2 3
HA3 1
HA4 4

What I'm looking to do is to replicate every row by the number in the 'count' column. So the output should look like this:

enter image description here

To be honest, I'm not a coder and so am trying to do this on a much larger dataset. Hoping to get some ideas from this community.

Thanks.

-Big_Ears


Solution

  • Try with reindex + repeat

    out = df.reindex(df.index.repeat(df['count']))