将列作为副本添加到 Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/18722536/
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
Adding a column to a Pandas DataFrame as a copy
提问by Roger
I have a pandas DataFrame called originaland I would like to add a new column to it and save the resultant DataFrame in a variable called modified. How do I do that?
我有一个名为original 的Pandas DataFrame ,我想向其中添加一个新列,并将生成的 DataFrame 保存在名为modified的变量中。我怎么做?
import pandas as pd
import numpy as np
original = pd.DataFrame(np.random.randn(5, 2), columns=['a', 'b'])
The solution given in the very similarly named questions here is to do something like:
此处名称非常相似的问题中给出的解决方案是执行以下操作:
original['c'] = original['b'].abs()
This does not work for me because it modifies the originalDataFrame. A potential solution is to use join, but that does not allow me to name it nor does it allow it be filled with a scalar values:
这对我不起作用,因为它修改了原始DataFrame。一个潜在的解决方案是使用 join,但这不允许我命名它,也不允许它填充标量值:
modified = original.join(original['b'].abs(),rsuffix='_abs')
The aim is to able to add the column in a single line without temp variables to achieve the following effect:
目的是能够在没有临时变量的情况下将列添加到一行中,以实现以下效果:
modified = original.some_op() \
    .a_different_op() \
    .add_a_column() \ # <- the step I can't figure out
    .another_op() \
    .final_op()
回答by wirrbel
Use pandas.DataFrame.assignmethod it is described here http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html
pandas.DataFrame.assign这里描述的使用方法http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html

