1

The question gets much clearer when an example is provided. I have a Data Frame with two columns, one of type string and one of type integer:

Col1   Col2
-------------
str1    2
str2    4
str3    1

now i need a list that contains the string in Col1 times the number in Col2, i.e. ['str1', 'str1', 'str2', 'str2', 'str2', 'str2', 'str3'].

What is the most efficient way to do so?

2
  • 3
    np.repeat(df['Col1'], df['Col2']). Commented Aug 2, 2019 at 15:18
  • wow, that was quick. thats it, thank you! Commented Aug 2, 2019 at 15:20

1 Answer 1

0
import numpy as np
np.repeat(df.Col1.values, df.Col2.values)

Output:

array(['str1', 'str1', 'str2', 'str2', 'str2', 'str2', 'str3'])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.