2

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
0

4 Answers 4

3

Use np.tile

>>> df['Y'] = np.tile(np.arange(1, 6), len(df))[:len(df)]
3

Doing

df['New'] = df.index%5 + 1
df
Out[21]: 
     X  Y  New
0   20  1    1
1   95  2    2
2   86  3    3
3   95  4    4
4    9  5    5
5   19  1    1
6    4  2    2
7   26  3    3
8   66  4    4
9   72  5    5
10  26  1    1
11   5  2    2
12   2  3    3
13  73  4    4
14  73  5    5
15  88  1    1
16  51  2    2
2
  • 1
    This is the most elegant solution ;) clean and easy to change... I would only suggest using df.reset_index().index or np.arange(len(df)) instead of df.index in case the DataFrame index is arbitrary
    – Rodalm
    Commented Jun 15, 2022 at 23:07
  • @Rodalm agree on that ~
    – BENY
    Commented Jun 16, 2022 at 0:14
2

itertools to the rescue.

>>> import itertools
>>> X = [20,95,86,95,9,19,4,26,66,72,26,5,2,73,73,88,51]
>>> result = list(zip(X, itertools.cycle((1,2,3,4,5))))
>>> result
[(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)]
>>>
0

One option with add_column from pyjanitor:

# pip install pyjanitor
import pandas as pd
import janitor

df.add_column(value = [1,2,3,4,5], column_name = 'new', fill_remaining=True)
Out[106]:
     X  Y  new
0   20  1    1
1   95  2    2
2   86  3    3
3   95  4    4
4    9  5    5
5   19  1    1
6    4  2    2
7   26  3    3
8   66  4    4
9   72  5    5
10  26  1    1
11   5  2    2
12   2  3    3
13  73  4    4
14  73  5    5
15  88  1    1
16  51  2    2

Not the answer you're looking for? Browse other questions tagged or ask your own question.