Put a 2d Array into a Pandas Series

前端 未结 2 2104
故里飘歌
故里飘歌 2021-02-12 18:07

I have a 2D Numpy array that I would like to put in a pandas Series (not a DataFrame):

>>> import pandas as pd
>>> import numpy as np
>>&         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-12 19:09

     pd.Series(list(a))
    

    is consistently slower than

    pd.Series(a.tolist())
    

    tested 20,000,000 -- 500,000 rows

    a = np.ones((500000,2))
    

    showing only 1,000,000 rows:

    %timeit pd.Series(list(a))
    1 loop, best of 3: 301 ms per loop
    
    %timeit pd.Series(a.tolist())
    1 loop, best of 3: 261 ms per loop
    

提交回复
热议问题