Add a Constant Value to All Rows in a Dataframe

Add column with constant value to pandas dataframe

The reason this puts NaN into a column is because df.index and the Index of your right-hand-side object are different. @zach shows the proper way to assign a new column of zeros. In general, pandas tries to do as much alignment of indices as possible. One downside is that when indices are not aligned you get NaN wherever they aren't aligned. Play around with the reindex and align methods to gain some intuition for alignment works with objects that have partially, totally, and not-aligned-all aligned indices. For example here's how DataFrame.align() works with partially aligned indices:

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

Add a constant value to all rows in a dataframe

Simply df + 1 will do that for you.

How to add a constant value to a column in python pandas?

You can do like below:

user['UID'] = 1

If just one row is getting filled, you can use ffill(). It will replicate the first row's value in all the rows.

user.UID = user.UID.ffill()

Add column to dataframe with constant value

df['Name']='abc' will add the new column and set all rows to that value:

In [79]:

df
Out[79]:
Date, Open, High, Low, Close
0 01-01-2015, 565, 600, 400, 450
In [80]:

df['Name'] = 'abc'
df
Out[80]:
Date, Open, High, Low, Close Name
0 01-01-2015, 565, 600, 400, 450 abc

How to add a constant value column to an empty dataframe?

You can do this if instead of relying on R to "recycle" the values the right number of times you explicitly use rep:

df = data.frame(x = numeric())
df['Country'] = rep("CHL", nrow(df))
df
# [1] x Country
# <0 rows> (or 0-length row.names)

df = data.frame(x = 1:3)
df['Country'] = rep("CHL", nrow(df))
df
# x Country
# 1 1 CHL
# 2 2 CHL
# 3 3 CHL

Add a column with a constant value to a DataFrame

A more general alternative is:

julia> insertcols!(df, :z => 1)
10×3 DataFrame
Row │ x y z
│ Int64 Char Int64
─────┼────────────────────
1 │ 1 a 1
2 │ 2 b 1
3 │ 3 c 1
4 │ 4 d 1
5 │ 5 e 1
6 │ 6 f 1
7 │ 7 g 1
8 │ 8 h 1
9 │ 9 i 1
10 │ 10 j 1

which by default does the same, but it additionally:

  1. allows you to specify the location of the new column;
  2. by default makes sure that you do not accidentally overwrite an existing column

Add constant list to pandas column

Simpler way is:

df['c'] =  [[7,8,9,10]]*len(df)

result:

   a  b              c
0 1 4 [7, 8, 9, 10]
1 2 5 [7, 8, 9, 10]
2 3 6 [7, 8, 9, 10]

UPDATE:

To avoid problem of shallow copy of lists in each row (as @YOBEN_S described), use:

df['c'] = df.apply(lambda x: [7,8,9,10], axis = 1)

Now it is possible to change for example only first element in column c of the first row by calling:

df.loc[0,'c'][0]='test'
   a  b                 c
0 1 4 [test, 8, 9, 10]
1 2 5 [7, 8, 9, 10]
2 3 6 [7, 8, 9, 10]


Related Topics



Leave a reply



Submit