Pandas Add Constant Column to DataFrame

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 22, 2023
Spread the love

In pandas you can add a new constant column with a literal value to DataFrame using assign() method, this method returns a new Dataframe after adding a column. insert() is also used to update the existing DataFrame with a new constant column. In this article, I will explain several ways of how to add a new column with a constant value to pandas DataFrame with examples.

1. Quick Examples of Add Constant Column to Pandas DataFrame

If you are in a hurry, below are some quick examples of how to add a constant column value to pandas DataFrame.


# Below are quick example
# Adding new column with a constant value
df["Discount_Percentage"] = 10

# Using DataFrame.insert() to add column constant value 
df.insert(1, 'Discount_Percentage', '10')

# Add a constant number to each column elements
df['Discount'] = df['Discount'] + 150

# Using DataFrame.apply() and lambda function
df['Discount_Percentage'] = df.apply(lambda x: 10, axis=1)

# Using DataFrame.assign() to add constant column
df2 = df.assign(Discount_Percentage=10)

# Add multiple constant columns
data = {'Discount_Percentage': 10, 'Advance': 1000}
df2 = df.assign(**data)

# Using pandas series
df['Discount_Percentage'] = pd.Series([10 for x in range(len(df.index))])

Now, let’s create a DataFrame with a few rows and columns, execute these examples and validate results. Our DataFrame contains column names Courses, Fee, Duration, and Discount.


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

Yields below output.


   Courses    Fee Duration  Discount
r1    Spark  20000   30days      1000
r2  PySpark  25000   40days      2300
r3   Python  22000   35days      1200
r4   pandas  30000   50days      2000

2. Pandas Add Column with Constant Value to DataFrame

You have an existing DataFrame where you need to add an additional column with the same constant value for every row. df["Discount_Percentage"]=10 will add the “Discount_Percentage” column and set every row with a constant value 10.


# Adding new column with a constant value
df["Discount_Percentage"] = 10
print(df)

Yields below output.


   Courses    Fee Duration  Discount  Discount_Percentage
0    Spark  20000   30days      1000                   10
1  PySpark  25000   40days      2300                   10
2   Python  22000   35days      1200                   10
3   pandas  30000   50days      2000                   10

3. Using DataFrame.insert() to Add Column Constant Value

DataFrame.insert() method is used to add a new column to DataFrame at any position of the existing DataFrame. Using this you can specify the index where you would like to add a column. The below example adds a constant column at the second position (Index 1). Note that in pandas, the Index starts from zero.

insert() method updates the existing DataFrame object with the new column.


# Using DataFrame.insert() to add column constant value 
df = pd.DataFrame(technologies,index=index_labels)
df.insert(1, 'Discount_Percentage', '10')
print(df)

Yields below output.


   Courses Discount_Percentage    Fee Duration  Discount
0    Spark                  10  20000   30days      1000
1  PySpark                  10  25000   40days      2300
2   Python                  10  22000   35days      1200
3   pandas                  10  30000   50days      2000

4. Add a Constant Number to Each Column Elements

You can use df['Discount']=df['Discount']+150 method to add 150 to Discount column. Using this approach you can add a value to inter columns or append a constant string to String columns.


# Add a constant number to each column elements
df = pd.DataFrame(technologies,index=index_labels)
df['Discount'] = df['Discount'] + 150
print(df)

Yields below output.


    Courses    Fee Duration  Discount
r1    Spark  20000   30days      1150
r2  PySpark  25000   40days      2450
r3   Python  22000   35days      1350
r4   pandas  30000   50days      2150

5. Using DataFrame.apply() and Lambda Function to Add Column Constant Value

Use DataFrame.apply() and lambda to create Discount_Percentage column with constant value 10.


# Using DataFrame.apply() and lambda function
df = pd.DataFrame(technologies,index=index_labels)
df['Discount_Percentage'] = df.apply(lambda x: 10, axis=1)
print(df)

Yields below output.


   Courses    Fee Duration  Discount  Discount_Percentage
0    Spark  20000   30days      1000                   10
1  PySpark  25000   40days      2300                   10
2   Python  22000   35days      1200                   10
3   pandas  30000   50days      2000                   10

6. Using DataFrame.assign() to Add Constant Column

DataFrame.assign() is also used to add a constant column to the pandas DataFrame, this method returns a new DataFrame after adding a "Discount_Percentage" column to the existing DataFrame.


# Using DataFrame.assign() to add constant column
df = pd.DataFrame(technologies,index=index_labels)
df2 = df.assign(Discount_Percentage=10)
print(df2)

Yields same output as above.

7. Add Multiple Constant Columns Using DataFrame.assign()

You can also use DataFrame.assign() method to add multiple constant columns to the pandas DataFrame. If you need to assign multiple columns with different values, you should use assign with a dictionary.


# Add multiple constant columns
data = {'Discount_Percentage': 10, 'Advance': 1000}
df2 = df.assign(**data)
print(df2)

Yields below output.


   Courses    Fee Duration  Discount  Discount_Percentage  Advance
0    Spark  20000   30days      1000                   10     1000
1  PySpark  25000   40days      2300                   10     1000
2   Python  22000   35days      1200                   10     1000
3   pandas  30000   50days      2000                   10     1000

8. Using Pandas Series


# Using pandas series
df = pd.DataFrame(technologies)
df['Discount_Percentage'] = pd.Series([10 for x in range(len(df.index))])
print(df)

Yields below output.


   Courses    Fee Duration  Discount  Discount_Percentage
0    Spark  20000   30days      1000                   10
1  PySpark  25000   40days      2300                   10
2   Python  22000   35days      1200                   10
3   pandas  30000   50days      2000                   10

9. Complete Example For Add Constant Column to DataFrame


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame(technologies,index=index_labels)
print(df)

# Adding new column with a constant value
df["Discount_Percentage"] = 10
print(df)

# Using DataFrame.insert() to add column constant value
df = pd.DataFrame(technologies,index=index_labels) 
df.insert(1, 'Discount_Percentage', '10')
print(df)

# Add a constant number to each column elements
df = pd.DataFrame(technologies,index=index_labels)
df['Discount'] = df['Discount'] + 150
print(df)

# Using DataFrame.apply() and lambda function
df = pd.DataFrame(technologies,index=index_labels)
df['Discount_Percentage'] = df.apply(lambda x: 10, axis=1)
print(df)

# Using DataFrame.assign() to add constant column
df = pd.DataFrame(technologies,index=index_labels)
df2 = df.assign(Discount_Percentage=10)
print(df2)

# Add multiple constant columns
df = pd.DataFrame(technologies,index=index_labels)
data = {'Discount_Percentage': 10, 'Advance': 1000}
df2 = df.assign(**data)
print(df2)

# Using pandas series
df['Discount_Percentage'] = pd.Series([10 for x in range(len(df.index))])
print(df)

Conclusion

In this article, you have learned how to add a constant column to pandas DataFrame by using DataFrame.assing(), DataFrame.insert(), pandas.Series(), DataFrame.apply() and Lambda function with examples.

Happy Learning !!

References

Leave a Reply

You are currently viewing Pandas Add Constant Column to DataFrame