How to Create a Column With Repeating Value in Python Dataframe
To create a column with repeating value in a Python dataframe, you can use [] square brackets along with the = assignment operator.
The following example shows how to create a column with repeating value in a Python dataframe using [] square brackets and the = assignment operator.
Using [] and = Operator
We can use [] square brackets and the = assignment operator to add a column with repeating values.
Suppose we have the following dataframe:
# Import pandas library
import pandas as pd
# Create dataframe
office_stuff = 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 with repeating values
office_stuff['Category'] = 'Office Supplies'
# Display the dataframe
print(office_stuff)
Output: 👇️
Date Product_Code Product_Name Price Status Category
0 01-03-2023 A-101 Laptop 4500 1 Office Supplies
1 01-03-2023 A-102 Mobile 550 1 Office Supplies
2 01-03-2023 A-103 Printer 250 1 Office Supplies
3 01-03-2023 B-101 Keyboard 50 0 Office Supplies
4 02-03-2023 B-102 Scanner 350 1 Office Supplies
5 02-03-2023 B-104 Mouse 50 1 Office Supplies
In this example, we use [] square brackets and the = assignment operator to add a new column Category with repeating values to the dataframe office_stuff.
Conclusion
We can use [] square brackets and the = assignment operator to create a column with repeating value in a Python dataframe.