Add Column to Pandas DataFrame with a Default Value



Add Column to Pandas DataFrame with a Default Value

The three ways to add a column to Pandas DataFrame with Default Value.

  • Using pandas.DataFrame.assign(**kwargs)
  • Using [] operator
  • Using pandas.DataFrame.insert()

Using Pandas.DataFrame.assign(**kwargs)

It Assigns new columns to a DataFrame and returns a new object with all existing columns to new ones. Existing columns that are re-assigned will be overwritten.

Syntax: Pandas.DataFrame.assign(**kwargs)

Parameters : **kwargsdict of {str: callable or Series}

Returns : DataFrame

Let’s understand with examples:

First, create a simple DataFrame.

# importing pandas as pd
import pandas as pd
 
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
 
                   "Address": ['Patna', 'Delhi', 'Coimbatore',
                               'Greater noida', 'Patna'],
 
                   "ID": [20123, 20124, 20145, 20146, 20147],
 
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
 
print("Original DataFrame :")
display(df)

Output:

Add a new column:

new_df = df.assign(profit=[40000, 20000, 30000, 60000, 200000])
new_df

Output:

 

Add a new column with Default Value:

new_df = df.assign(profit='NAN')
new_df

Output:

Using [] operator to add a new column

We can use DataFrame indexing to create a new column in DataFrame and set it to default values.

Syntax:

df[col_name]=value

Let’s understand with an example:

# importing pandas as pd
import pandas as pd
 
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                    
                   "Address": ['Patna', 'Delhi', 'Coimbatore'
                               'Greater noida', 'Patna'],
                    
                   "ID": [20123, 20124, 20145, 20146, 20147],
                    
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
 
print("Original DataFrame :")
display(df)

Output:

 

Add new column in Dataframe:

df['loss'] = [40000, 20000, 30000, 60000, 200000]
df

Output:

Add a new column with default values:

df['loss'] = 'NAN'
df

Output:

Using pandas.DataFrame.insert()

Add new column into DataFrame at specified location.

 

Syntax: DataFrame.insert(loc, column, value, allow_duplicates=False)

Parameters

loc : int Insertion index. Must verify 0 <= loc <= len(columns).

column : str, number, or hashable object Label of the inserted column.

value : int, Series, or array-like

allow_duplicates : bool, optional

Let’s understand with examples:

# importing pandas as pd
import pandas as pd
 
# creating the dataframe
df = pd.DataFrame({"Name": ['Anurag', 'Manjeet', 'Shubham',
                            'Saurabh', 'Ujjawal'],
                    
                   "Address": ['Patna', 'Delhi', 'Coimbatore'
                               'Greater noida', 'Patna'],
                    
                   "ID": [20123, 20124, 20145, 20146, 20147],
                    
                   "Sell": [140000, 300000, 600000, 200000, 600000]})
 
print("Original DataFrame :")
display(df)

Output:

Add a new column with default value:

df.insert(2, "expenditure", 4500, allow_duplicates=False)
df

Output:

 

Last Updated on October 20, 2021 by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended Blogs