Is it possible to add several columns at once to a pandas DataFrame?

后端 未结 4 1728
走了就别回头了
走了就别回头了 2020-12-01 13:55

If I want to create a new DataFrame with several columns, I can add all the columns at once -- for example, as follows:

data = {\'col_1\': [0, 1, 2, 3],
             


        
4条回答
  •  悲&欢浪女
    2020-12-01 14:42

    All you need to do is create the new columns with data from the additional dataframe.

    data =            {'col_1': [0, 1, 2, 3],
                       'col_2': [4, 5, 6, 7]}
    additional_data = {'col_3': [8, 9, 10, 11],
                       'col_4': [12, 13, 14, 15]}
    df = pd.DataFrame(data)
    df2 = pd.DataFrame(additional_data)
    
    df[df2.columns] = df2
    

    df now looks like:

       col_1  col_2  col_3  col_4
    0      0      4      8     12
    1      1      5      9     13
    2      2      6     10     14
    3      3      7     11     15
    

    Indices from the original dataframe will be used as if you had performed an in-place left join. Data from the original dataframe in columns with a matching name in the additional dataframe will be overwritten. For example:

    data =            {'col_1': [0, 1, 2, 3],
                       'col_2': [4, 5, 6, 7]}
    additional_data = {'col_2': [8, 9, 10, 11],
                       'col_3': [12, 13, 14, 15]}
    df = pd.DataFrame(data)
    df2 = pd.DataFrame(additional_data, index=[0,1,2,4])
    
    df[df2.columns] = df2
    

    df now looks like:

       col_1  col_2  col_3
    0      0      8     12
    1      1      9     13
    2      2     10     14
    3      3    NaN    NaN
    

提交回复
热议问题