How to duplicate Python dataframe one by one?

后端 未结 5 1692
面向向阳花
面向向阳花 2021-01-05 13:01

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:

5条回答
  •  耶瑟儿~
    2021-01-05 13:11

    I do not know if it is more efficient than your loop, but it easy enough to construct as:

    Code:

    pd.concat([df] * 3).sort_index()
    

    Test Code:

    df = pd.DataFrame([[1, 2], [3, 4]], columns=list('ab'))
    print(pd.concat([df] * 3).sort_index())
    

    Results:

       a  b
    0  1  2
    0  1  2
    0  1  2
    1  3  4
    1  3  4
    1  3  4
    

提交回复
热议问题