Adding new column to existing DataFrame in Python pandas Adding new column to existing DataFrame in Python pandas python python

Adding new column to existing DataFrame in Python pandas


Edit 2017

As indicated in the comments and by @Alexander, currently the best method to add the values of a Series as a new column of a DataFrame could be using assign:

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

Edit 2015
Some reported getting the SettingWithCopyWarning with this code.
However, the code still runs perfectly with the current pandas version 0.16.1.

>>> sLength = len(df1['a'])>>> df1          a         b         c         d6 -0.269221 -0.026476  0.997517  1.2943858  0.917438  0.847941  0.034235 -0.448948>>> df1['e'] = pd.Series(np.random.randn(sLength), index=df1.index)>>> df1          a         b         c         d         e6 -0.269221 -0.026476  0.997517  1.294385  1.7571678  0.917438  0.847941  0.034235 -0.448948  2.228131>>> pd.version.short_version'0.16.1'

The SettingWithCopyWarning aims to inform of a possibly invalid assignment on a copy of the Dataframe. It doesn't necessarily say you did it wrong (it can trigger false positives) but from 0.13.0 it let you know there are more adequate methods for the same purpose. Then, if you get the warning, just follow its advise: Try using .loc[row_index,col_indexer] = value instead

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

In fact, this is currently the more efficient method as described in pandas docs


Original answer:

Use the original df1 indexes to create the series:

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


This is the simple way of adding a new column: df['e'] = e


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.)

I assume that the index values in e match those in df1.

The easiest way to initiate a new column named e, and assign it the values from your series e:

df['e'] = e.values

assign (Pandas 0.16.0+)

As of Pandas 0.16.0, you can also use assign, which assigns new columns to a DataFrame and returns a new object (a copy) with all the original columns in addition to the new ones.

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

As per this example (which also includes the source code of the assign function), you can also include more than one column:

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_b0  1  3     1.5     3.51  2  4     1.5     3.5

In context with your example:

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         d0  1.764052  0.400157  0.978738  2.2408932 -0.103219  0.410599  0.144044  1.4542743  0.761038  0.121675  0.443863  0.3336747  1.532779  1.469359  0.154947  0.3781639  1.230291  1.202380 -0.387327 -0.302303>>> e0   -1.0485531   -1.4200182   -1.7062703    1.9507754   -0.509652dtype: float64df1 = df1.assign(e=e.values)>>> df1          a         b         c         d         e0  1.764052  0.400157  0.978738  2.240893 -1.0485532 -0.103219  0.410599  0.144044  1.454274 -1.4200183  0.761038  0.121675  0.443863  0.333674 -1.7062707  1.532779  1.469359  0.154947  0.378163  1.9507759  1.230291  1.202380 -0.387327 -0.302303 -0.509652

The description of this new feature when it was first introduced can be found here.