Add a new column to Pandas DataFrame

In this lesson, we will learn how to add a new column to Pandas DataFrame in Python. We will insert a column to an already created DataFrame. Let us see various ways to achieve this:

  1. Add a new column using the insert() method
  2. Assign a new column using the assign() method

Add a new column using insert()

To insert a new column into a DataFrame, use the insert() method. We can set the location, name, and the values as a parameter:

The 1st parameter of the insert() is the location you want the new column. The 2nd is the name of the new column and 3rd are the values of the new column.

Let us see an example of adding a new column to a Pandas DataFrame using the insert() method:

Output

Assign a new column using assign()

To assign new columns to an already created Pandas DataFrame, use the assign() method. It adds a new column to the end. Set the new column with the column name as a parameter of the assign() method:

Let us see an example of adding a new column to a Pandas Dataframe using the assign() method:

Output


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


 

Read Excel in Python Pandas
Delete rows/ columns in a Pandas DataFrame
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading