lets say I have a brand of list:
cat_list = ['a', 'b', 'ab']
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:
type value category
a 25 a
a 25 b
a 25 ab
b 50 a
b 50 b
b 50 ab
What I have tried so far is:
cat_list = ['a', 'b', 'ab']
df['category'] = cat_list * len(df)
but I got this kind of error
Length of values does not match length of index
how should I fix my script in order to get the desired results?
thanks.
CodePudding user response:
Use numpy.tile
by repeat with integer division for number of repeats:
df = pd.DataFrame({'a':range(8)})
cat_list = ['a', 'b', 'ab']
df['category'] = np.tile(cat_list, (len(df.index) // len(cat_list)) 1)[:len(df.index)]
print (df)
a category
0 0 a
1 1 b
2 2 ab
3 3 a
4 4 b
5 5 ab
6 6 a
7 7 b