Add multiple empty columns to pandas DataFrame

后端 未结 7 1283
名媛妹妹
名媛妹妹 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:19

    Why not just use loop:

    for newcol in ['B','C','D']:
        df[newcol]=np.nan
    
    0 讨论(0)
  • 2020-11-27 13:22

    Just to add to the list of funny ways:

    columns_add = ['a', 'b', 'c']
    df = df.assign(**dict(zip(columns_add, [0] * len(columns_add)))
    
    0 讨论(0)
  • 2020-11-27 13:28

    I'd use

    df["B"], df["C"], df["D"] = None, None, None
    

    or

    df["B"], df["C"], df["D"] = ["None" for a in range(3)]
    
    0 讨论(0)
  • 2020-11-27 13:29

    Summary of alternative solutions:

    columns_add = ['a', 'b', 'c']
    
    1. for loop:

      for newcol in columns_add:
          df[newcol]= None
      
    2. dict method:

      df.assign(**dict([(_,None) for _ in columns_add]))
      
    3. tuple assignment:

      df['a'], df['b'], df['c'] = None, None, None
      
    0 讨论(0)
  • 2020-11-27 13:34

    If you don't want to rewrite the name of the old columns, then you can use reindex:

    df.reindex(columns=[*df.columns.tolist(), 'new_column1', 'new_column2'], fill_value=0)
    

    Full example:

    In [1]: df = pd.DataFrame(np.random.randint(10, size=(3,1)), columns=['A'])
    
    In [1]: df
    Out[1]: 
       A
    0  4
    1  7
    2  0
    
    In [2]: df.reindex(columns=[*df.columns.tolist(), 'col1', 'col2'], fill_value=0)
    Out[2]: 
    
       A  col1  col2
    0  1     0     0
    1  2     0     0
    

    And, if you already have a list with the column names, :

    In [3]: my_cols_list=['col1','col2']
    
    In [4]: df.reindex(columns=[*df.columns.tolist(), *my_cols_list], fill_value=0)
    Out[4]: 
       A  col1  col2
    0  1     0     0
    1  2     0     0
    
    0 讨论(0)
  • 2020-11-27 13:37

    You could use df.reindex to add new columns:

    In [18]: df = pd.DataFrame(np.random.randint(10, size=(5,1)), columns=['A'])
    
    In [19]: df
    Out[19]: 
       A
    0  4
    1  7
    2  0
    3  7
    4  6
    
    In [20]: df.reindex(columns=list('ABCD'))
    Out[20]: 
       A   B   C   D
    0  4 NaN NaN NaN
    1  7 NaN NaN NaN
    2  0 NaN NaN NaN
    3  7 NaN NaN NaN
    4  6 NaN NaN NaN
    

    reindex will return a new DataFrame, with columns appearing in the order they are listed:

    In [31]: df.reindex(columns=list('DCBA'))
    Out[31]: 
        D   C   B  A
    0 NaN NaN NaN  4
    1 NaN NaN NaN  7
    2 NaN NaN NaN  0
    3 NaN NaN NaN  7
    4 NaN NaN NaN  6
    

    The reindex method as a fill_value parameter as well:

    In [22]: df.reindex(columns=list('ABCD'), fill_value=0)
    Out[22]: 
       A  B  C  D
    0  4  0  0  0
    1  7  0  0  0
    2  0  0  0  0
    3  7  0  0  0
    4  6  0  0  0
    
    0 讨论(0)
提交回复
热议问题