Adding new column to existing DataFrame in Python pandas

后端 未结 25 1298
你的背包
你的背包 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:30

    For the sake of completeness - yet another solution using DataFrame.eval() method:

    Data:

    In [44]: e
    Out[44]:
    0    1.225506
    1   -1.033944
    2   -0.498953
    3   -0.373332
    4    0.615030
    5   -0.622436
    dtype: float64
    
    In [45]: df1
    Out[45]:
              a         b         c         d
    0 -0.634222 -0.103264  0.745069  0.801288
    4  0.782387 -0.090279  0.757662 -0.602408
    5 -0.117456  2.124496  1.057301  0.765466
    7  0.767532  0.104304 -0.586850  1.051297
    8 -0.103272  0.958334  1.163092  1.182315
    9 -0.616254  0.296678 -0.112027  0.679112
    

    Solution:

    In [46]: df1.eval("e = @e.values", inplace=True)
    
    In [47]: df1
    Out[47]:
              a         b         c         d         e
    0 -0.634222 -0.103264  0.745069  0.801288  1.225506
    4  0.782387 -0.090279  0.757662 -0.602408 -1.033944
    5 -0.117456  2.124496  1.057301  0.765466 -0.498953
    7  0.767532  0.104304 -0.586850  1.051297 -0.373332
    8 -0.103272  0.958334  1.163092  1.182315  0.615030
    9 -0.616254  0.296678 -0.112027  0.679112 -0.622436
    

提交回复
热议问题