| by Arround The Web | No comments

Pandas Add Column with Default Values

There are a number of libraries and modules available in Python that are used for data analysis and manipulation operations. The “Pandas” is a popular library that is used to perform data operations on large or small groups of data. While handling data in Python, sometimes we need to add data with default values for an entire table or DataFrame. To add/insert a column with default values in Pandas DataFrame, various methods are utilized in Python.

This write-up will present a comprehensive guide on adding a Pandas DataFrame column with default values to the existing DataFrame.

How to Add/Insert Column With Default Values in Pandas DataFrame?

To add a column with default values in Pandas DataFrame, the below methods are used in Python:

Method 1: Add Column With Default Values in Pandas DataFrame Using “df[ ]” Operator

The “df[ ]” operator is used to add a column with default values in Pandas DataFrame. Let’s understand it via the following examples:

Example 1: Adding Column With Same Default Value

In the below example code, the “pandas.DataFrame()” function of the “pandas” module is used to create a DataFrame. After creating DataFrame, the “df[ ]” operator is used to add the new column with the same default value.

import pandas
data1 = {'Name':["Joseph","Lily","Henry","Tim"],'Id_No:' :[1802,1805,1808,1809],'Age':[21, 15, 18, 19]}
data_frame = pandas.DataFrame(data1)
print(data_frame, '\n')
data_frame['Remarks'] = 'Pass'
print(data_frame)

The above code generates the following DataFrame that contains the new column with the default value:

Example 2: Adding Column With Different Default Value

The below code adds a new column named “Remarks” with a different default value for each row using the “df[ ]” operator.

import pandas
data1 = {'Name':["Joseph","Lily","Henry","Tim"],'Id_No:' :[1802,1805,1808,1809],'Age':[21, 15, 18, 19]}
data_frame = pandas.DataFrame(data1)
print(data_frame, '\n')
data_frame['Remarks'] = ['Pass','Fail','Fail','Pass']
print(data_frame)

This code displays the following output to the console:

Method 2: Add Column With Default Values in Pandas DataFrame Using “DataFrame.assign()”

The “DataFrame.assign()” method is used to add a new DataFrame column to an existing DataFrame. This method can be utilized to add/insert a new column with a default value in Pandas DataFrame.

Here in the following code, the “df.assign()” method is used to add the column name “Country” to the Pandas DataFrame with the default value “USA”:

import pandas
data1 = {'Name':["Joseph","Lily","Henry","Tim"],'Id_No:' :[1802,1805,1808,1809],'Age':[21, 15, 18, 19]}
df = pandas.DataFrame(data1)
print(df)
df2 = df.assign(Country='USA')
print('\n',df2)

When the above code is executed, the below output is shown to the console:

Method 3: Add Column with Default Values in Pandas DataFrame Using “DataFrame.insert()”

The “DataFrame.insert()” method can also be used to insert the new column in Python. We can utilize this method to add columns with default values in DataFrame.

In the below code, the “DataFrame.insert()” method takes the location, column name, column value, and allow_duplicates parameter as an argument and adds a new column with Default values to DataFrame. The “allow_duplicates=False” value specifies that the DataFrame does not add columns with a similar name:

import pandas
data1 = {'Name':["Joseph","Lily","Henry","Tim"],'Id_No:' :[1802,1805,1808,1809],'Age':[21, 15, 18, 19]}
data_frame = pandas.DataFrame(data1)
print(data_frame, '\n')
data_frame.insert(3, "Remarks", "Pass", allow_duplicates=False)
print(data_frame)

The above code execution returns the following output:

That’s all about adding columns with default values in Pandas DataFrame.

Conclusion

The “df[ ]” operator, “DataFrame.assign()”, and “DataFrame.insert()” methods are used to add a column with default values in Pandas DataFrame. These methods are used to add columns with the same default value or different default values to existing Pandas DataFrame. This tutorial delivered an in-depth guide on how to add a column with default values using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply