Thursday, December 26, 2024
Google search engine
HomeLanguagesAdd column with constant value to pandas dataframe

Add column with constant value to pandas dataframe

Prerequisite: PandasĀ 

In this article, we will learn how to add a new column with constant value to a Pandas DataFrame. Before that one must be familiar with the following concepts:

  • Pandas DataFrame : Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular arrangement with labeled axes (rows and columns). A Data frame may be a two-dimensional arrangement , i.e., data is aligned during a tabular fashion in rows and columns. Pandas DataFrame consists of three principal components, the data, rows, and columns.
  • Column in DataFrame : In Order to pick a column in Pandas DataFrame, we will either access the columns by calling them by their columns name. Column Addition: so as to feature a column in Pandas DataFrame, we will declare a replacement list as a column and increase a existing Dataframe.
  • Constant : A fixed value. In Algebra, a continuing may be a number on its own, or sometimes a letter like a, b or c to face for a hard and fast number. Example: in ā€œx + 5 = 9ā€, 5 and 9 are constants.

Approach

  1. Import Library
  2. Load or create a dataframe
  3. Add column with constant value to dataframe

To understand these above mentioned steps, lets discuss some examples :

Example 1: (By using Pandas Series)

Python3




# import packages
import pandas as pd
import numpy as np
Ā Ā 
# create dataframe
df = pd.DataFrame({'Number': {0: 1, 1: 2, 2: 3, 3: 4, 4: 5},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 'Power 2': {0: 1, 1: 4, 2: 9, 3: 16, 4: 25},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 'Power 3': {0: 1, 1: 8, 2: 27, 3: 64, 4: 125}})
Ā Ā 
# view dataframe
print("Initial dataframe")
display(df)
Ā Ā 
Ā Ā 
# adding column with constant value
df['Power 0'] = pd.Series([1 for x in range(len(df.index))])
Ā Ā 
# view dataframe
print("Final dataframe")
display(df)


Output :

Example 2: (As static value)

Python3




# import packages
import pandas as pd
import numpy as np
Ā Ā 
# create dataframe
df = pd.DataFrame({'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash', 3: 'Aman', 4: 'Akash'},
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 'Marks': {0: 68, 1: 87, 2: 45, 3: 78, 4: 56}})
Ā Ā 
Ā Ā 
# view dataframe
print("Initial dataframe")
display(df)
Ā Ā 
Ā Ā 
# adding column with constant value
df['Pass'] = True
Ā Ā 
# view dataframe
print("Final dataframe")
display(df)


Output :

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?