python del vs pandas drop

后端 未结 4 2222
孤街浪徒
孤街浪徒 2021-02-13 05:45

I know it might be old debate, but out of pandas.drop and python del function which is better in terms of performance over large dataset?

I am

4条回答
  •  抹茶落季
    2021-02-13 06:09

    Using randomly generated data of about 1.6 GB, it appears that df.drop is faster than del, especially over multiple columns:

    df = pd.DataFrame(np.random.rand(20000,10000))
    t_1 = time.time()
    df.drop(labels=[2,4,1000], inplace=True)
    t_2 = time.time()
    print(t_2 - t_1)
    

    0.9118959903717041

    Compared to:

    df = pd.DataFrame(np.random.rand(20000,10000))
    t_3 = time.time()
    del df[2]
    del df[4]
    del df[1000]
    t_4 = time.time()
    print(t_4 - t_3)
    

    4.052732944488525

    @Inder's comparison is not quite the same since it doesn't use inplace=True.

提交回复
热议问题