In This Section we will be focusing on how to add a column with default values in pandas dataframe, There are multiple ways to do it, like adding a column with default value at the first position and last position (start and end of the dataframe) , adding multiple columns with default values to the dataframe in pandas. let’s look at each of these cases in pandas with an example for each.
- Assign or replace existing column with empty or null values in pandas python
- Add a column with default values in pandas dataframe.
- Add a column with default values at first position of the dataframe in pandas (start of the dataframe)
- Add multiple columns with default values in pandas (to end of the dataframe)
- Add multiple columns with default values to the start of the dataframe in pandas
Create Dataframe:
## create dataframe import pandas as pd import numpy as np #Create a DataFrame import pandas as pd import numpy as np d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'], 'Age':[26,23,23,23,23,24], 'Score':[85,31,55,55,31,77], 'City':['Newyork','Seattle','Toronto','California','Delhi','Mumbai']} df = pd.DataFrame(d,columns=['Name','Age','Score','City']) df
The Resultant dataframe is
Add new column with default values in pandas: Method 1
In the below example we have added the new column with default value in pandas
#### add new column with default values in pandas: Method 1 df["University"] = 'MIT' df
As the result University column is added to the pandas dataframe with default value –“MIT” as shown below
Add new column with default values in pandas: Method 2
In the below example we have added the new column with default value in pandas, we have used lambda function to assign the default value throughout the column
#### add new column with default values in pandas: Method 2 df["University"] = df.apply(lambda _: 'MIT', axis=1) df
As the result University column is added to the pandas dataframe with default value –“MIT” as shown below
Add new column with default values in pandas: Method 3
In the below example we have added the new column with default value in pandas, we have used assign function to assign the default value throughout the column
#### add new column with default values in pandas: Method 3 df2 = df.assign(University="MIT") df2
As the result University column is added to the pandas dataframe with default value –“MIT” as shown below
Add new column with default values to start of the dataframe in pandas
In the below example we have added the new column with default values to the start of the dataframe .i.e. to the first position of dataframe in pandas, we have used insert function to assign the default value to the first position
#### add new column to 1st position of the dataframe in pandas: Method 1 df.insert(0,"University", "MIT") df
As the result University column is added to 1st position of the pandas dataframe with default value –“MIT” as shown below
Add multiple columns with default values in pandas dataframe
In the below example we have added the multiple columns with default values to the dataframe . we have used assign function to assign the default values multiple columns at once.
#### add multiple columns with default values in pandas : Method 1 df2 = df.assign(University="MIT", University_code = 3014) df2
As the result University column and University_code column is created in the default value is created at once as shown below
pandas Add multiple columns with default values to start of the dataframe
In the below example we have added the multiple columns with default values to the start of the dataframe. .i.e. to the first position of dataframe in pandas, we have used insert function to assign the default value to the first position
#### add multiple columns with default values in pandas at start of the dataframe df.insert(0,"University", "MIT") df.insert(1,"University_code", 3014) df
As the result University column and University_code column is created to the start of the dataframe, with the default value is created at once as shown below