lets say I have a brand of list:
JavaScript
x
2
1
cat_list = ['a', 'b', 'ab']
2
and I want this list to repeteadly fill a new column called category
as much as my rows have.
I want the first row have 'a'
, the second row have 'b'
, and the third row have 'ab'
, and the cycle repeats until the last rows like the example below:
JavaScript
1
8
1
type value category
2
a 25 a
3
a 25 b
4
a 25 ab
5
b 50 a
6
b 50 b
7
b 50 ab
8
What I have tried so far is:
JavaScript
1
3
1
cat_list = ['a', 'b', 'ab']
2
df['category'] = cat_list * len(df)
3
but I got this kind of error
JavaScript
1
2
1
Length of values does not match length of index
2
how should I fix my script in order to get the desired results?
thanks.
Advertisement
Answer
Use numpy.tile
by repeat with integer division for number of repeats:
JavaScript
1
15
15
1
df = pd.DataFrame({'a':range(8)})
2
3
cat_list = ['a', 'b', 'ab']
4
df['category'] = np.tile(cat_list, (len(df.index) // len(cat_list)) + 1)[:len(df.index)]
5
print (df)
6
a category
7
0 0 a
8
1 1 b
9
2 2 ab
10
3 3 a
11
4 4 b
12
5 5 ab
13
6 6 a
14
7 7 b
15