How can I construct a Pandas DataFrame from individual 1D Numpy arrays without copying

后端 未结 3 1568
青春惊慌失措
青春惊慌失措 2021-02-19 23:07

Unlike every other question I can find, I do not want to create a DataFrame from a homogeneous Numpy array, nor do I want to convert a structured array into a DataFrame.

3条回答
  •  不要未来只要你来
    2021-02-19 23:59

    I don't think this fully answers the question but it might help.

    1-when you initialize your dataframe directly from 2D array, a copy is not made.

    2-you don't have 2D arrays, you have 1D arrays, how do you get 2D arrays from 1D arrays without making copies, I don't know.

    To illustrate the points, see below:

    a = np.array([1,2,3])
    b = np.array([4,5,6])
    c = np.array((a,b))
    df = pd.DataFrame(c)
    a = np.array([1,2,3])
    b = np.array([4,5,6])
    c = np.array((a,b))
    df = pd.DataFrame(c)
    
    print(c)
    [[1 2 3]
     [4 5 6]]
    
    print(df)
       0  1  2
    0  1  2  3
    1  4  5  6
    
    c[1,1]=10
    print(df)
       0   1  2
    0  1   2  3
    1  4  10  6
    

    So, changing c indeed changes df. However if you try changing a or b, that does not affect c (or df).

提交回复
热议问题