Add constant value column to pandas data frame

Given a DataFrame:

np.random.seed(0) df = pd.DataFrame(np.random.randn(3, 3), columns=list('ABC'), index=[1, 2, 3]) df ABC 1 1.764052 0.400157 0.978738 2 2.240893 1.867558 -0.977278 3 0.950088 -0.151357 -0.103219 

What is the easiest way to add a new column containing a constant value, like 0?

  ABC new 1 1.764052 0.400157 0.978738 0 2 2.240893 1.867558 -0.977278 0 3 0.950088 -0.151357 -0.103219 0 



This is my solution, but I don’t know why it puts NaN in the β€œnew” column?

 df['new'] = pd.Series([0 for x in range(len(df.index))]) ABC new 1 1.764052 0.400157 0.978738 0.0 2 2.240893 1.867558 -0.977278 0.0 3 0.950088 -0.151357 -0.103219 NaN 
+66
python pandas
Jun 04 '14 at 13:39 on
source share
3 answers

The reason NaN is placed in a column is because the df.index and Index your right object are different. @zach shows the correct way to assign a new column of zeros. In general, pandas tries to maximize index alignment. One drawback is that when the indices are not aligned, you get NaN wherever they are aligned. Play with reindex and align methods to get some intuition for alignment by working with objects that have partially, fully and not aligned all aligned indexes. For example, here DataFrame.align() works with partially aligned indexes:

 In [7]: from pandas import DataFrame In [8]: from numpy.random import randint In [9]: df = DataFrame({'a': randint(3, size=10)}) In [10]: In [10]: df Out[10]: a 0 0 1 2 2 0 3 1 4 0 5 0 6 0 7 0 8 0 9 0 In [11]: s = df.a[:5] In [12]: dfa, sa = df.align(s, axis=0) In [13]: dfa Out[13]: a 0 0 1 2 2 0 3 1 4 0 5 0 6 0 7 0 8 0 9 0 In [14]: sa Out[14]: 0 0 1 2 2 0 3 1 4 0 5 NaN 6 NaN 7 NaN 8 NaN 9 NaN Name: a, dtype: float64 
+18
Jun 04 '14 at 14:29
source share

Super simple: direct assignment

For on-site modification, perform a direct assignment. This assignment is broadcast by pandas for each row.

 df = pd.DataFrame('x', index=range(4), columns=list('ABC')) df ABC 0 xxx 1 xxx 2 xxx 3 xxx 

 df['new'] = 'y' # Same as, # df.loc[:, 'new'] = 'y' df ABC new 0 xxxy 1 xxxy 2 xxxy 3 xxxy 



Create Copy: DataFrame.assign

If you need a copy, use DataFrame.assign :

 df.assign(new='y') ABC new 0 xxxy 1 xxxy 2 xxxy 3 xxxy 

And if you need to assign several such columns with the same value, it is as simple as,

 c = ['new1', 'new2', ...] df.assign(**dict.fromkeys(c, 'y')) ABC new1 new2 0 xxxyy 1 xxxyy 2 xxxyy 3 xxxyy 

Multiple Column Assignment
Finally, if you need to assign multiple columns with different values, you can use assign with a dictionary.

 c = {'new1': 'w', 'new2': 'y', 'new3': 'z'} df.assign(**c) ABC new1 new2 new3 0 xxxwyz 1 xxxwyz 2 xxxwyz 3 xxxwyz 
+17
Jan 24 '19 at 11:47
source share

Here is another insert using lambdas (create a column with a constant value = 10)

 df['newCol'] = df.apply(lambda x: 10, axis=1) 

before

 df ABC 1 1.764052 0.400157 0.978738 2 2.240893 1.867558 -0.977278 3 0.950088 -0.151357 -0.103219 

after

 df ABC newCol 1 1.764052 0.400157 0.978738 10 2 2.240893 1.867558 -0.977278 10 3 0.950088 -0.151357 -0.103219 10 
+1
Aug 14 '19 at 19:58
source share



All Articles