Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicating a Pandas DF N times

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?

like image 882
redrubia Avatar asked Jan 27 '14 15:01

redrubia


2 Answers

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]
like image 136
capitalistcuttle Avatar answered Oct 31 '22 14:10

capitalistcuttle


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 = 4
n_df = pd.concat([df] * n)

Then n_df is the following DataFrame:

    id  temp    name
row         
1   34  null    mark
2   22  null    mark
3   34  null    mark
1   34  null    mark
2   22  null    mark
3   34  null    mark
1   34  null    mark
2   22  null    mark
3   34  null    mark
1   34  null    mark
2   22  null    mark
3   34  null    mark
like image 22
Dr Fabio Gori Avatar answered Oct 31 '22 13:10

Dr Fabio Gori