Method 1: Using Assign()
Assign() in Pandas DataFrame adds a column with default value in the existing DataFrame. We just need to pass the column name and set the default value to it.
Syntax:
Example 1:
Let’s create a DataFrame with two columns. Add a new column which is “review” and add the default string which is “GOOD”.
things=pandas.DataFrame({‘Name’:[‘Solar dish’,‘glasses’,‘oil’], ‘Purchased Status’:[1,0,0]})
print(things)
# Add ‘review’ column to the above dataframe with default string – “GOOD”
things=things.assign(review=“GOOD”)
print()
print(things)
Output:
Explanation:
We have two columns – “Name” and “Purchased Status” – in the things DataFrame. After adding the review column to it, you can see that the column is added at the last position of the DataFrame and all the values in this column are default.
Example 2:
Let’s create a DataFrame with two columns. Add a new column which is “ratings” and add the default value of 11.
things=pandas.DataFrame({‘Name’:[‘Solar dish’,‘glasses’,‘oil’], ‘Purchased Status’:[1,0,0]})
# Add ‘ratings’ column to the above dataframe with default value – 10
things=things.assign(ratings=10)
# Add ‘id’ column to the above dataframe with default value – 11
things=things.assign(id=11)
print(things)
Output:
Explanation:
After adding the rating column to it, you can see that the column is added at the last position of the DataFrame and all the values in this column are default – 11.
Method 2: Using []
The [ ] is an index operator that gets the values from the DataFrame column. If we pass the new column name inside it and set the default value, the new column is added to the existing Pandas DataFrame.
Syntax:
Example 1:
Let’s create a DataFrame with one column which is “items” with two values. Now, add a new column, “price”, with the default value.
ps=pandas.DataFrame({‘items’:[‘item-one’,‘item-two’]})
print(ps)
# Add ‘price’ column to the above dataframe with default value – 100
ps[‘price’]=100
print()
print(ps)
Output:
Explanation:
We have a column in the DataFrame. After adding the price column to it, you can see that the column is added at the last position of the DataFrame and both values in this column are default – 100.
Example 2:
Add a new column which is “label” with the default string as “packaged”.
ps=pandas.DataFrame({‘items’:[‘item-one’,‘item-two’]})
# Add ‘label’ column to the above dataframe with default string – “packaged”.
ps[‘label’]=“packaged”
print(ps)
Output:
Explanation:
We have a column in the DataFrame. After adding the label column to it, you can see that the column is added at the last position of the DataFrame and both values in this column are default – “packaged”.
Method 3: Using Insert()
Until now, a new column is inserted at the last position by default. Using the insert() method, you can add at any position in the existing DataFrame. It takes three parameters:
- The first parameter is the index position in which the column is inserted.
- The second parameter is the column name.
- The third parameter is the default value which is assigned to the column.
- The last parameter is to allow/reject the duplicates. If it is True, the duplicates are allowed. If it is False, the duplicates are not allowed.
Syntax:
Example 1:
Insert the “label” column to the previous DataFrame at first the position with the default string – “packaged”.
ps=pandas.DataFrame({‘items’:[‘item-one’,‘item-two’]})
# Insert ‘label’ column to the above dataframe at first position with default string – “packaged”.
ps.insert(0,“label”,“packaged”)
print(ps)
Output:
Explanation:
We have a column at index-0 which is “items”. Now, we insert the “label” column at index-0. After printing the DataFrame, the “label” is at the first position with the default value as “packaged”.
Example 2:
- Insert the “delivered” column to the previous DataFrame at the first position with the default string, “Yes”.
- Insert the “status” column to the previous DataFrame at the third position with the default value of 2.
ps=pandas.DataFrame({‘items’:[‘item-one’,‘item-two’]})
# Insert ‘delivered’ column to the above dataframe at first position with default string – “Yes”.
ps.insert(0,“delivered”,“Yes”)
print(ps)
# Insert ‘status’ column to the above dataframe at first position with default value – 2.
ps.insert(2,“status”,2)
print()
print(ps)
Output:
Explanation:
- First, we insert the “delivered” column to the previous DataFrame at the first position with the default string, “Yes”.
- Again, we insert the “status” column to the previous DataFrame at the first position with the default value of 2. The final column order is [“delivered”,”items”,”status”].
Conclusion
The methods to add the column with the default value are thoroughly discussed in a very simple way. The main objective of this tutorial is to help you know the idea of adding a column with the default value in Pandas. We discussed the three methods in this tutorial to add a column with a constant value. These are rather simple methods to add the columns in Pandas with a default value.