Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to duplicate Python dataframe one by one? [duplicate]

I have a pandas.DataFrame as follows:

df1 = 
    a    b
0   1    2
1   3    4

I'd like to make this three times to become:

df2 =
    a    b
0   1    2
0   1    2
0   1    2
1   3    4
1   3    4
1   3    4

df2 is made from a loop, but it is not efficient.

How can I get df2 from df1 using a matrix way which is faster?

like image 248
李博洋 Avatar asked May 07 '17 00:05

李博洋


1 Answers

Build a one dimensional indexer to slice both the the values array and index. You must take care of the index as well to get your desired results.

  • use np.repeat on an np.arange to get the indexer
  • construct a new dataframe using this indexer on both values and the index

r = np.arange(len(df)).repeat(3)
pd.DataFrame(df.values[r], df.index[r], df.columns)

   a  b
0  1  2
0  1  2
0  1  2
1  3  4
1  3  4
1  3  4
like image 190
piRSquared Avatar answered Oct 09 '22 01:10

piRSquared