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
CodePudding user response:
Use np.tile
>>> df['Y'] = np.tile(np.arange(1, 6), len(df))[:len(df)]
CodePudding user response:
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)]
>>>
CodePudding user response:
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
CodePudding user response:
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