How to Create a New Column with False Values to a DataFrame Using Python

Adding a new column with False values to a DataFrame is a common task in data manipulation using Python’s pandas library.

In this article, we cover two methods:

  1. Direct Indexing
  2. Using the assign() Function

Using Direct Indexing

Direct indexing allows you to add a new column by specifying its name and assigning values.

Below is an example of adding a column named New_column with False values to a DataFrame:

# Import pandas library  
import pandas as pd  

# Define DataFrame  
df = pd.DataFrame({  
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],  
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],  
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],  
    'Price': [4500, 550, 250, 50, 350, 50],  
    'Status': [1, 1, 1, 0, 1, 1]  
})  

# Add new column to DataFrame  
df["New_column"] = False  

# Show updated DataFrame  
print(df)  

Output: 👇️

         Date Product_Code Product_Name  Price  Status  New_column
0  01-03-2023        A-101       Laptop   4500       1       False
1  01-03-2023        A-102       Mobile    550       1       False
2  01-03-2023        A-103      Printer    250       1       False
3  01-03-2023        B-101     Keyboard     50       0       False
4  02-03-2023        B-102      Scanner    350       1       False
5  02-03-2023        B-104        Mouse     50       1       False

In this example, we use direct indexing to add a new column New_column with false values to the dataframe df.

Using assign() Function

The assign() function is another approach to add columns to a DataFrame. It integrates seamlessly into functional pipelines, making it ideal for chaining operations.

Suppose we have the following dataframe:

Here’s how to use the assign() function:

# Import pandas library  
import pandas as pd  

# Define DataFrame  
df = pd.DataFrame({  
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],  
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],  
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],  
    'Price': [4500, 550, 250, 50, 350, 50],  
    'Status': [1, 1, 1, 0, 1, 1]  
})  

# Add new column to DataFrame  
df = df.assign(New_column=False)  

# Show updated DataFrame  
print(df)  

Output: 👇️

         Date Product_Code Product_Name  Price  Status  new_column
0  01-03-2023        A-101       Laptop   4500       1       False
1  01-03-2023        A-102       Mobile    550       1       False
2  01-03-2023        A-103      Printer    250       1       False
3  01-03-2023        B-101     Keyboard     50       0       False
4  02-03-2023        B-102      Scanner    350       1       False
5  02-03-2023        B-104        Mouse     50       1       False

In this example, we use the assign() function to add a new column new_column with false values to the dataframe df. The output shows the updated dataframe with the new column.

Conclusion

Both direct indexing and the assign() function are effective methods for adding a new column with False values to a pandas DataFrame.