Linux Hint Posted July 24, 2023 Share Posted July 24, 2023 Python’s Pandas library provides a number of modules and functions for analyzing data and manipulation. While working with Pandas, sometimes we need to add/insert a specified column with a constant value to an existing DataFrame. A constant value is a value that does not change or vary across the rows of the DataFrame. Adding a column with a constant value can be useful for labeling, filtering, grouping, or merging your data. This Python blog will explain a detailed guide on adding columns with a constant value to a particular DataFrame. How to Add/Insert Columns With Constant Value in Pandas? The below approaches are used to add columns with constant values in Pandas DataFrame: Using the “Square Bracket” Using the “insert()” Using the “assign()” Using the “apply()” Method 1: Add Column With Constant Value in Pandas DataFrame Using the “Square Bracket” The square bracket notation adds columns with constant values in Pandas DataFrame. The following example is used to add a column with a constant value in Pandas DataFrame: import pandas data1 = ({'Name':["Anna","Jon","Cyndy"], 'roll_no' :[12, 15, 17],'marks':[50, 82, 94]}) data_frame1 = pandas.DataFrame(data1) print('Given DataFrame:\n', data_frame1) data_frame1["Total"] = 100 print('\n', data_frame1) In the above code, first, the “pandas” library is imported, and the “pandas.DataFrame()” function is utilized to create/construct a DataFrame. After that, the “data_frame1[“Total”] = 100” syntax is used to add the columns with the constant value “100” to the specified data frame. Output The above snippet shows that the column with a constant value has been added. Method 2: Add Column With Constant Value in Pandas DataFrame Utilizing the “df.insert()” The “dataframe.insert()” method inserts the DataFrame at the specified location. This method adds the column with contrast value in Pandas DataFrame. The working of the “df.insert()” method for adding a column with a constant value to Pandas is demonstrated in the following code snippet: import pandas data1 = ({'Name':["Anna","Jon","Cyndy"], 'roll_no' :[12, 15, 17], 'obtained_marks':[50, 82, 94]}) data_frame1 = pandas.DataFrame(data1) print('Given DataFrame:\n', data_frame1) data_frame1.insert(2, 'Total_marks', '100') print('\n', data_frame1) Here, the “df.insert()” takes the specified index value “2”, column name “Total_marks”, and the constant value “100” as arguments and adds the column with a constant value to the Pandas DataFrame. Output The column with constant value has been added to the specific location of the Pandas DataFrame. Method 3: Add Column With Constant Value in Pandas DataFrame Utilizing the “df.assign()” The “df.assign()” method can also be used to add/insert a column with a constant value to the DataFrame. Let’s overview the following example: import pandas data1 = ({'Name':["Anna","Jon","Cyndy"], 'roll_no' :[12, 15, 17], 'obtained_marks':[50, 82, 94]}) data_frame1 = pandas.DataFrame(data1) print('Given DataFrame:\n', data_frame1) df = data_frame1.assign(Total_marks=100) print('\n', df) In the above code, the “df.assign()” takes the column name with constant value as an argument and retrieves the DataFrame by adding a column. Output In the above snippet, the Pandas DataFrame has been updated with the column value with a constant value. Method 4: Add Column With Constant Value in DataFrame Utilizing the “df.apply()” In Python, the “apply()” method is utilized to apply a specified function along one of the DataFrame axis or index row axis. The below code is used to add a column with a constant value in Pandas DataFrame: import pandas data1 = ({'Name':["Anna","Jon","Cyndy"], 'roll_no' :[12, 15, 17],'obtained_marks':[50, 82, 94]}) data_frame1 = pandas.DataFrame(data1) print('Given DataFrame:\n', data_frame1) data_frame1['Total_marks'] = data_frame1.apply(lambda x: 100, axis = 1) print('\n', data_frame1) Here, the “df.apply()” method takes the “lambda” function as an argument and adds a specified column with a constant value to the DataFrame. Output The specified column has been added to the given DataFrame. Conclusion The “Square Bracket”, “df.insert()”, “df.assign()”, and the “df.apply()” methods are utilized to insert/add a column with a constant/fixed value to DataFrame. These methods can add specific columns with constant values to the desired location in the Pandas DataFrame. This blog has presented a detailed guide on adding a column with a constant value to the specified location. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.