Adding new column to existing DataFrame in Python pandas

后端 未结 25 1280
你的背包
你的背包 2020-11-22 01:15

I have the following indexed DataFrame with named columns and rows not- continuous numbers:

          a         b         c         d
2  0.671399  0.101208 -         


        
25条回答
  •  灰色年华
    2020-11-22 01:22

    I was looking for a general way of adding a column of numpy.nans to a dataframe without getting the dumb SettingWithCopyWarning.

    From the following:

    • the answers here
    • this question about passing a variable as a keyword argument
    • this method for generating a numpy array of NaNs in-line

    I came up with this:

    col = 'column_name'
    df = df.assign(**{col:numpy.full(len(df), numpy.nan)})
    

提交回复
热议问题