• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:19 mins read
You are currently viewing Pandas Add Constant Column to DataFrame

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.


# Quick examples of add constant column to pandas dataframe 

# Example 1: Adding new column with a constant value
df["Discount_Percentage"] = 10

# Example 2: Using DataFrame.insert() 
# To add column constant value 
df.insert(1, 'Discount_Percentage', '10')

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

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

# Example 5: Using DataFrame.assign() 
# To add constant column
df2 = df.assign(Discount_Percentage=10)

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

# Example 7: Use a Pandas Series to add a constant column
df['Discount_Percentage'] = pd.Series([10] * len(df.index))

# Example 8: 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.


# Create Pandas 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("Create DataFrame:\n",df)

Yields below output.

pandas add constant column

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("\nDataFrame after adding Discount_Percentage column:\n", df)

Yields below output.

pandas add constant column

The “Discount_Percentage” column has been successfully added with a constant value of 10 for all rows.

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

Alternatively, 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.


# 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

In the above example, df.insert(1, "Discount_Percentage", 10) inserts a new column named “Discount_Percentage” with a constant value of 10 at position 1 in the DataFrame.

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.


# 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 a constant value 10. For instance, the lambda function takes each row (specified by axis=1) and adds the constant value (10) to the ‘Discount_Percentage’ column for each row. Adjust the constant value or column name as needed for your specific use case.


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


# 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

Similarly, 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.

In this example, df.assign(Discount_Percentage=10) creates a new column named ‘Discount_Percentage’ with a constant value of 10 for all rows in the DataFrame. Adjust the constant value or column name as needed for your specific use case.


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

Yields the 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.

In the below example, the **data syntax is used to unpack the dictionary keys and values as keyword arguments for the assign() method, effectively adding multiple constant columns to the DataFrame.


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

Yields below output.


# 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 a Pandas Series to add a constant column to a DataFrame. For instance, a Pandas Series with constant values [10] is created and assigned to the ‘Discount_Percentage’ column in the DataFrame. The length of the Series is set to match the length of the DataFrame using len(df.index). Adjust the column name and constant values as needed for your specific use case.


# Use a Pandas Series to add a constant column
df['Discount_Percentage'] = pd.Series([10] * len(df.index))
print(df)

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


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

# Use a Pandas Series to add a constant column
df['Discount_Percentage'] = pd.Series([10] * len(df.index))
print(df)

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

Frequently Asked Questions on Add Constant Column to DataFrame

How can I add a constant column to a Pandas DataFrame?

To add a constant column to a Pandas DataFrame, you can simply assign a scalar value to a new column. For example, a new column named ‘Country’ is added to the DataFrame, and its values are set to the constant value ‘USA’.

Can I add a constant column with different values for each row?

You can add a constant column with different values for each row using the assignment operator or the DataFrame.insert() method. Use a Pandas Series or a list to provide different constant values

Can I add a constant column at a specific position in the DataFrame?

You can add a constant column at a specific position in the DataFrame using the insert method. For example, the insert method is used to add a new column named ‘Gender’ with the constant value ‘Female’ at position 1 (index 1).

How do I add a constant column using the assign method?

To add a constant column to a Pandas DataFrame using the assign method. For example, assign is used to create a new DataFrame (df) with the added column named ‘new_column’ and the specified constant value. The original DataFrame (df) remains unchanged.

How can I add a constant column with NaN values?

To add a constant column with NaN values to a Pandas DataFrame. For instance, uses NumPy’s np.nan to represent NaN values, and it assigns this constant value to a new column named ‘new_column’ in your DataFrame. Adjust the column name and DataFrame variable according to your specific use case.

Can I add a constant column based on a condition?

You can add a constant column to a Pandas DataFrame based on a condition using the numpy library and boolean indexing.

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

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium

Leave a Reply