course content

Course Content

Pandas First Steps

Add a New Column 1/2Add a New Column 1/2

We have learned how to create a dataframe. Now is the time to see what we can do with them.

Let's first create a small dataframe with three columns and seven rows.

We can add columns to the created dataframe. Moreover, we can do this in several ways; we will consider two of them. The syntax of the first method is as follows:

dataframe['name of the new column'] = [value_1, value_2, value_3]

Here, dataframe is the name of our already created dataframe to which we are going to add new columns.

name of the new column is the name of the column you add to the dataframe.

value_1, value_2, value_3 are the values ​​that will be in the new column.

Note the following: the name of the new column is enclosed in quotation marks and placed in square brackets. Accordingly, the values ​​added to the column are also placed in square brackets. If the values are of the numeric data type, then they are not quoted; if the values are of the string data type, then they are quoted.

Now it's time to see how we add a population column to the already created countries dataframe.

Note that this way the new column is added to the end.

Section 1.

Chapter 7