How to add new column?
In Pandas, adding a new column to an existing DataFrame
Let's look at how to add new columns to an existing Pandas DataFrame. This task can be accomplished in a variety of ways.
The first method involves declaring a new list as a column.
# Add the pandas package to your project.
pandas as a pd import
# Create a dictionary containing information about students.
'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], data =
[5.1, 6.2, 5.1, 5.2], [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc', 'Msc', 'Msc', 'Msc']
# Create a DataFrame from the vocabulary.
pd.DataFrame = df (data)
# Create a list that will be turned into a column.
['Delhi', 'Bangalore', 'Chennai', 'Patna'] address
# Using the column name 'Address' and equating it to the list
address = df['Address']
# Take note of the df Output:
It's important to note that the length of your list must equal the length of the index column; otherwise, an error will appear.
The second method is to use DataFrame.insert ()
It allows us to add a column at any point in the document, not just at the end. It also offers a variety of alternatives for entering column values.
# Add the pandas package to your project.
pandas as a pd import
# Create a dictionary containing information about students.
'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], data =
[5.1, 6.2, 5.1, 5.2], [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc', 'Msc', 'Msc', 'Msc']
# Create a DataFrame from the vocabulary.
pd.DataFrame = df (data)
# Adding a column with DataFrame.insert()
df.insert df.insert df.inser (2, "Age", [21, 23, 24, 21], True)
# Take note of the df Output:
The third way is to use the Dataframe.assign() method.
This method will generate a new dataframe with the previous dataframe's new column added to it.
# Add the pandas package to your project.
pandas as a pd import
# Create a dictionary containing information about students.
'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], data =
[5.1, 6.2, 5.1, 5.2], [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc', 'Msc', 'Msc', 'Msc']
# Create a DataFrame from the vocabulary.
pd.DataFrame = df (data)
# Using the column name 'Address' and equating it to the list
df2 = df.assign(address = ['Delhi, Bengaluru, Chennai, Patna])
# Take a look at the df2 output:
The fourth method is to use a dictionary.
To add a new column to a pandas DataFrame, we can use a Python dictionary. Use an existing column as the key value, and the values in that column will be the new column's values.
# Add the pandas package to your project.
pandas as a pd import
# Create a dictionary containing information about students.
'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'], data =
[5.1, 6.2, 5.1, 5.2], [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc', 'Msc', 'Msc', 'Msc']
# Create a dictionary with the key values of an existing column and their corresponding # value pairs as our new column's values.
address = 'Jai' in Delhi, 'Princi' in Bangalore,
'Gaurav' in Patna, 'Anuj' in Chennai
# Create a DataFrame from the vocabulary.
pd.DataFrame = df (data)
# Set the column name to 'Address'.
address = df['Address']
# Take note of the output df Output:
Pay attention, nerd! Learn the fundamentals of Python programming and strengthen your foundations with the Python Programming Foundation Course.
Mtmats mtmats mtmats mtmats mtmats undefined undefined undefined undefined undefined undefined
Comments
Post a Comment