Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycling in Pandas Dataframe

When filling an array with values from a shorter array in R, R will "recycle" the elements in the shorter array. For example, setting an array of length 7 [ , , , , , , ] with the array ['a','b','c'] will give ['a','b','c','a','b','c','a'].

Is there a built-in method to fill a pandas column (or numpy array) using a similar style of recycling?

like image 386
Thomas Matthew Avatar asked Jun 20 '18 20:06

Thomas Matthew


1 Answers

With numpy.resize -

In [199]: a = ['a','b','c']

In [200]: np.resize(a,7)
Out[200]: array(['a', 'b', 'c', 'a', 'b', 'c', 'a'], dtype='|S1')
like image 119
Divakar Avatar answered Oct 21 '22 17:10

Divakar