Repeating each item a different number of times

Issue

I have a dataframe with a column with item names and a column with a number. I would like to create a list with the item names repeated the number of times in the column.

item number
cat 2
dog 3
parrot 4

My desired output is

item
cat
cat
dog
dog
dog
parrot
parrot
parrot
parrot

I feel like I’m quite close with this code:

for index in df.iterrows():
    for x in range(2):
        print(df.item)

However, I can’t find a way to replace 2 in range with the number out of the dataframe. df.numbers doesn’t seem to work.

Solution

As you said, your desired output is a list, using @Michael’s comment, you can do this:

list(df.item.repeat(df.number))

The output would be:

['cat', 'cat', 'dog', 'dog', 'dog', 'parrot', 'parrot', 'parrot', 'parrot']

Answered By – Shayan

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.