0

I have the following datafame:

count   color   name
2       blue    mike
3       red     james   
1       orange  mark    
3       yellow  oscar

I want to take the count value and insert as many rows as the count value. The output should be:

count   color   name
2       blue    mike
2       blue    mike
3       red     james
3       red     james
3       red     james   
1       orange  mark    
3       yellow  oscar
3       yellow  oscar
3       yellow  oscar

I've tried to use the multiply function, but this is just multiplying the count column when I want to insert more rows. If someone could point me in the right direction, I would greatly appreciate it.

ApacheOne
  • 245
  • 2
  • 14

1 Answers1

0

Try with reindex and repeat

df = df.reindex(df.index.repeat(df['count']))
BENY
  • 317,841
  • 20
  • 164
  • 234