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

后端 未结 3 1558
青春惊慌失措
青春惊慌失措 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:52

    May I suggest adding the columns one by one. It might help with efficiency. Like this for example,

    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame()
    
    col1 = np.array([1, 2, 3])
    col2 = np.array([4, 5, 6])
    
    df['col1'] = col1
    df['col2'] = col2
    
    >>> df
       col1  col2
    0     1     4
    1     2     5
    2     3     6
    

提交回复
热议问题