pandas 熊猫分配新的列名作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39767718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:06:29  来源:igfitidea点击:

pandas assign with new column name as string

pythonpandasassigncolumnname

提问by FLab

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

我最近发现了Pandas的“分配”方法,我觉得它非常优雅。我的问题是新列的名称被指定为关键字,因此其中不能有空格或破折号。

df = DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})
df.assign(ln_A = lambda x: np.log(x.A))
        A         B      ln_A
0   1  0.426905  0.000000
1   2 -0.780949  0.693147
2   3 -0.418711  1.098612
3   4 -0.269708  1.386294
4   5 -0.274002  1.609438
5   6 -0.500792  1.791759
6   7  1.649697  1.945910
7   8 -1.495604  2.079442
8   9  0.549296  2.197225
9  10 -0.758542  2.302585

but what if I want to name the new column "ln(A)" for example? E.g.

但是,例如,如果我想将新列命名为“ln(A)”怎么办?例如

df.assign(ln(A) = lambda x: np.log(x.A))
df.assign("ln(A)" = lambda x: np.log(x.A))


File "<ipython-input-7-de0da86dce68>", line 1
df.assign(ln(A) = lambda x: np.log(x.A))
SyntaxError: keyword can't be an expression

I know I could rename the column right after the .assign call, but I want to understand more about this method and its syntax.

我知道我可以在 .assign 调用后立即重命名该列,但我想更多地了解此方法及其语法。

回答by Piotr

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

您可以将关键字参数assign作为字典传递给,如下所示:

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

回答by piRSquared

assignexpects a bunch of key word arguments. It will, in turn, assign columns with the names of the key words. That's handy, but you can't pass an expression as the key word. This is spelled out by @EdChum in the comments with this link

assign期望一堆关键字参数。反过来,它会为列分配关键字的名称。这很方便,但您不能将表达式作为关键字传递。@EdChum 在此链接的评论中详细说明了这一点

use insertinstead for inplace transformation

使用insert代替就地转换

df.insert(2, 'ln(A)', np.log(df.A))
df

enter image description here

在此处输入图片说明



use concatif you don't want inplace

使用concat如果你不想就地

pd.concat([df, np.log(df.A).rename('log(A)')], axis=1)

enter image description here

在此处输入图片说明