How to duplicate Python dataframe one by one?

后端 未结 5 1562
面向向阳花
面向向阳花 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:22

    You can use np.repeat

    df = pd.DataFrame(np.repeat(df.values,[3,3], axis = 0), columns = df.columns)
    

    You get

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

    Time testing:

    %timeit pd.DataFrame(np.repeat(df.values,[3,3], axis = 0))
    1000 loops, best of 3: 235 µs per loop
    
    %timeit pd.concat([df] * 3).sort_index()
    best of 3: 1.26 ms per loop
    

    Numpy is definitely faster in most cases so no surprises there

    EDIT: I am not sure if you would be looking for repeating indices but incase you do,

    pd.DataFrame(np.repeat(df.values,3, axis = 0), index = np.repeat(df.index, 3), columns = df.columns)
    

提交回复
热议问题