Duplicating a Pandas DF N times
list, pandas, python
Solution
Actually, since you want to duplicate the entire dataframe (and not each element), numpy.tile() may be better:
In [69]: import pandas as pd
In [70]: arr = pd.np.array([[1, 2, 3], [4, 5, 6]])
In [71]: arr
Out[71]:
array([[1, 2, 3],
[4, 5, 6]])
In [72]: df = pd.DataFrame(pd.np.tile(arr, (5, 1)))
In [73]: df
Out[73]:
0 1 2
0 1 2 3
1 4 5 6
2 1 2 3
3 4 5 6
4 1 2 3
5 4 5 6
6 1 2 3
7 4 5 6
8 1 2 3
9 4 5 6
[10 rows x 3 columns]
In [75]: df = pd.DataFrame(pd.np.tile(arr, (1, 3)))
In [76]: df
Out[76]:
0 1 2 3 4 5 6 7 8
0 1 2 3 1 2 3 1 2 3
1 4 5 6 4 5 6 4 5 6
[2 rows x 9 columns]
Problem
So right now, if I multiple a list i.e. `x = [1,2,3]* 2 I get x as [1,2,3,1,2,3]` But this doesn't work with Pandas. So if I want to duplicate a PANDAS DF I have to make a column a list and multiple: ``` col_x_duplicates = list(df['col_x'])*N new_df = DataFrame(col_x_duplicates, columns=['col_x']) ``` Then do a join on the original data: ``` pd.merge(new_df, df, on='col_x', how='left') ``` This now duplicates the pandas DF N times, Is there an easier way? Or even a quicker way?