Add Column to Pandas DataFrame with a Default Value - GeeksforGeeks

29 Aug, 2020
The three ways to add a column to Pandas DataFrame with Default Value.
Using pandas.DataFrame.assign(**kwargs)
Using [] operator
Using pandas.DataFrame.insert()
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.
Python3
# importing pandas as pdimport pandas as pd  # creating the dataframedf = 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:
Python3
new_df = df.assign(profit=[40000, 20000, 30000, 60000, 200000])new_df
Output:
Add a new column with Default Value:
Python3
new_df = df.assign(profit='NAN')new_df
Output:
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:
Python3
# importing pandas as pdimport pandas as pd  # creating the dataframedf = 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:
Python3
df['loss'] = [40000, 20000, 30000, 60000, 200000]df
Output:
Add a new column with default values:
Python3
df['loss'] = 'NAN'df
Output:
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:
Python3
# importing pandas as pdimport pandas as pd  # creating the dataframedf = 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:
Python3
df.insert(2, "expenditure", 4500, allow_duplicates=False)df
Output:
Python pandas-dataFrame
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python