在Python pandas中将新列添加到现有DataFrame

2024-04-25 21:40:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下带命名列和行的索引数据帧-非连续数字:

          a         b         c         d
2  0.671399  0.101208 -0.181532  0.241273
3  0.446172 -0.243316  0.051767  1.577318
5  0.614758  0.075793 -0.451460 -0.012493

我想在现有数据帧中添加一个新列'e',并且不想更改数据帧中的任何内容(即,新列的长度始终与数据帧的长度相同)。

0   -0.335485
1   -1.166658
2   -0.385571
dtype: float64

我尝试了joinappendmerge的不同版本,但没有得到我想要的结果,最多只有错误。如何将列e添加到上述示例中?


Tags: 数据版本示例内容错误数字merge命名
3条回答

I would like to add a new column, 'e', to the existing data frame and do not change anything in the data frame. (The series always got the same length as a dataframe.)

我假设e中的索引值与df1中的索引值匹配。

启动名为e的新列并为其分配序列e中的值的最简单方法是:

df['e'] = e.values

分配(熊猫0.16.0+)

从Pandas 0.16.0开始,您还可以使用^{},它将新列分配给一个DataFrame,并返回一个新对象(一个副本),除了新列之外,还返回所有原始列。

df1 = df1.assign(e=e.values)

根据this example(还包括assign函数的源代码),还可以包含多个列:

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
>>> df.assign(mean_a=df.a.mean(), mean_b=df.b.mean())
   a  b  mean_a  mean_b
0  1  3     1.5     3.5
1  2  4     1.5     3.5

结合你的例子:

np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(10, 4), columns=['a', 'b', 'c', 'd'])
mask = df1.applymap(lambda x: x <-0.7)
df1 = df1[-mask.any(axis=1)]
sLength = len(df1['a'])
e = pd.Series(np.random.randn(sLength))

>>> df1
          a         b         c         d
0  1.764052  0.400157  0.978738  2.240893
2 -0.103219  0.410599  0.144044  1.454274
3  0.761038  0.121675  0.443863  0.333674
7  1.532779  1.469359  0.154947  0.378163
9  1.230291  1.202380 -0.387327 -0.302303

>>> e
0   -1.048553
1   -1.420018
2   -1.706270
3    1.950775
4   -0.509652
dtype: float64

df1 = df1.assign(e=e.values)

>>> df1
          a         b         c         d         e
0  1.764052  0.400157  0.978738  2.240893 -1.048553
2 -0.103219  0.410599  0.144044  1.454274 -1.420018
3  0.761038  0.121675  0.443863  0.333674 -1.706270
7  1.532779  1.469359  0.154947  0.378163  1.950775
9  1.230291  1.202380 -0.387327 -0.302303 -0.509652

这个新特性首次引入时的描述可以在here中找到。

使用原始df1索引创建序列:

df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)

编辑2015
有人报告说用这段代码得到了SettingWithCopyWarning 但是,代码仍然与当前的pandas版本0.16.1完美地运行。

>>> sLength = len(df1['a'])
>>> df1
          a         b         c         d
6 -0.269221 -0.026476  0.997517  1.294385
8  0.917438  0.847941  0.034235 -0.448948

>>> df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)
>>> df1
          a         b         c         d         e
6 -0.269221 -0.026476  0.997517  1.294385  1.757167
8  0.917438  0.847941  0.034235 -0.448948  2.228131

>>> p.version.short_version
'0.16.1'

SettingWithCopyWarning旨在通知数据帧副本上可能无效的赋值。它不一定说你做错了(它可以触发误报),但从0.13.0开始,它让你知道有更多的方法可以达到同样的目的。然后,如果收到警告,只需遵循其建议:尝试使用.loc[row_index,col_indexer]=value而不是

>>> df1.loc[:,'f'] = pd.Series(np.random.randn(sLength), index=df1.index)
>>> df1
          a         b         c         d         e         f
6 -0.269221 -0.026476  0.997517  1.294385  1.757167 -0.050927
8  0.917438  0.847941  0.034235 -0.448948  2.228131  0.006109
>>> 

实际上,这是目前更有效的方法,如described in pandas docs


编辑2017

如注释和@Alexander所示,当前将序列值添加为数据帧的新列的最佳方法可能是使用^{}

df1 = df1.assign(e=pd.Series(np.random.randn(sLength)).values)

这是添加新列的简单方法:df['e'] = e

相关问题 更多 >