Duplicating a Pandas DF N times Duplicating a Pandas DF N times pandas pandas

Duplicating a Pandas DF N times


Actually, since you want to duplicate the entire dataframe (and not each element), numpy.tile() may be better:

In [69]: import pandas as pdIn [70]: arr = pd.np.array([[1, 2, 3], [4, 5, 6]])In [71]: arrOut[71]: array([[1, 2, 3],       [4, 5, 6]])In [72]: df = pd.DataFrame(pd.np.tile(arr, (5, 1)))In [73]: dfOut[73]:    0  1  20  1  2  31  4  5  62  1  2  33  4  5  64  1  2  35  4  5  66  1  2  37  4  5  68  1  2  39  4  5  6[10 rows x 3 columns]In [75]: df = pd.DataFrame(pd.np.tile(arr, (1, 3)))In [76]: dfOut[76]:    0  1  2  3  4  5  6  7  80  1  2  3  1  2  3  1  2  31  4  5  6  4  5  6  4  5  6[2 rows x 9 columns]


Here is a one-liner to make a DataFrame with n copies of DataFrame df

n_df = pd.concat([df] * n)

Example:

df = pd.DataFrame(    data=[[34, 'null', 'mark'], [22, 'null', 'mark'], [34, 'null', 'mark']],     columns=['id', 'temp', 'name'],     index=pd.Index([1, 2, 3], name='row'))n = 4n_df = pd.concat([df] * n)

Then n_df is the following DataFrame:

    id  temp    namerow         1   34  null    mark2   22  null    mark3   34  null    mark1   34  null    mark2   22  null    mark3   34  null    mark1   34  null    mark2   22  null    mark3   34  null    mark1   34  null    mark2   22  null    mark3   34  null    mark