Question:
Let’s say we have two variables X and Y. I want to generate Y by repeating a set of numbers (1, 2, 3, 4, 5) or 1:5 over the rows. How can we generate column Y?X Y
20 1
95 2
86 3
95 4
9 5
19 1
4 2
26 3
66 4
72 5
26 1
5 2
2 3
73 4
73 5
88 1
51 2
Answer:
Usenp.tile
>>> df['Y'] = np.tile(np.arange(1, 6), len(df))[:len(df)]
If you have better answer, please add a comment about this, thank you!