Add multiple empty columns to pandas DataFrame

后端 未结 7 1284
名媛妹妹
名媛妹妹 2020-11-27 12:39

How do I add multiple empty columns to a DataFrame from a list?

I can do:

    df["B"] = N         


        
相关标签:
7条回答
  • 2020-11-27 13:41

    I'd concat using a DataFrame:

    In [23]:
    df = pd.DataFrame(columns=['A'])
    df
    
    Out[23]:
    Empty DataFrame
    Columns: [A]
    Index: []
    
    In [24]:    
    pd.concat([df,pd.DataFrame(columns=list('BCD'))])
    
    Out[24]:
    Empty DataFrame
    Columns: [A, B, C, D]
    Index: []
    

    So by passing a list containing your original df, and a new one with the columns you wish to add, this will return a new df with the additional columns.


    Caveat: See the discussion of performance in the other answers and/or the comment discussions. reindex may be preferable where performance is critical.

    0 讨论(0)
提交回复
热议问题