Add Column with Constant Value to Pandas 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 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 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()

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

how to add a constant column to a dataframe without rows

You can use .loc specifying the row index and column label, as follows:

df.loc[0, 'foo'] = 'bar'

Result:

print(df)

a b c foo
0 NaN NaN NaN bar

You can also use:

df['foo'] = ['bar']

Result:

print(df)

a b c foo
0 NaN NaN NaN bar

If you have a bunch of a mix of empty and non-empty dataframes and you want to assign new column to it, you can try the following code:

df['foo'] = ['bar'] * (df.shape[0] if df.shape[0] else 1)

This will assign the constant with the same length (number of rows) for non-empty dataframes and will also assign one new row for empty dataframe with the constant value for the 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]

Adding multiple constant values in a pandas dataframe column

You can use numpy.repeat with the DataFrame constructor:

vals = [5,10,30,100]
reps = [3,2,6,1]
df = pd.DataFrame({'A': np.repeat(vals, reps)})
df.index+=1

output:

      A
1 5
2 5
3 5
4 10
5 10
6 30
7 30
8 30
9 30
10 30
11 30
12 100


Related Topics



Leave a reply



Submit