pandas assign with new column name as string

后端 未结 2 489
情歌与酒
情歌与酒 2020-12-05 03:51

I recently discovered pandas \"assign\" method which I find very elegant. My issue is that the name of the new column is assigned as keyword, so it cannot have spaces or das

2条回答
  •  孤城傲影
    2020-12-05 04:46

    You can pass the keyword arguments to assign as a dictionary, like so:

    kwargs = {"ln(A)" : lambda x: np.log(x.A)}
    df.assign(**kwargs)
    
        A         B     ln(A)
    0   1  0.500033  0.000000
    1   2 -0.392229  0.693147
    2   3  0.385512  1.098612
    3   4 -0.029816  1.386294
    4   5 -2.386748  1.609438
    5   6 -1.828487  1.791759
    6   7  0.096117  1.945910
    7   8 -2.867469  2.079442
    8   9 -0.731787  2.197225
    9  10 -0.686110  2.302585
    

提交回复
热议问题