How to repeat Pandas data frame? How to repeat Pandas data frame? python python

How to repeat Pandas data frame?


You can use the concat function:

In [13]: pd.concat([x]*5)Out[13]:    a  b0  1  20  1  20  1  20  1  20  1  2

If you only want to repeat the values and not the index, you can do:

In [14]: pd.concat([x]*5, ignore_index=True)Out[14]:    a  b0  1  21  1  22  1  23  1  24  1  2


I think it's cleaner/faster to use iloc nowadays:

In [11]: np.full(3, 0)Out[11]: array([0, 0, 0])In [12]: x.iloc[np.full(3, 0)]Out[12]:   a  b0  1  20  1  20  1  2

More generally, you can use tile or repeat with arange:

In [21]: df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"])In [22]: dfOut[22]:   A  B0  1  21  3  4In [23]: np.tile(np.arange(len(df)), 3)Out[23]: array([0, 1, 0, 1, 0, 1])In [24]: np.repeat(np.arange(len(df)), 3)Out[24]: array([0, 0, 0, 1, 1, 1])In [25]: df.iloc[np.tile(np.arange(len(df)), 3)]Out[25]:   A  B0  1  21  3  40  1  21  3  40  1  21  3  4In [26]: df.iloc[np.repeat(np.arange(len(df)), 3)]Out[26]:   A  B0  1  20  1  20  1  21  3  41  3  41  3  4

Note: This will work with non-integer indexed DataFrames (and Series).


Try using numpy.repeat:

>>> df=pd.DataFrame(pd.np.repeat(x.values,5,axis=0),columns=x.columns)>>> df   a  b0  1  21  1  22  1  23  1  24  1  2>>>